windows下安裝nodejs及架構express_node.js

來源:互聯網
上載者:User

以下通過兩種方式介紹windows下安裝node.js及架構express

第一種方式:

總共分為四步輕鬆完成Nodejs的Express安裝

第一步、安裝

所有要先按裝express-generator,否則建立項目時,會提示express命令沒找到

npm install -g express-generator #需先安裝express-generatornpm install -g expressexpress -V #驗證是否安裝成功

第二步、建立項目

$ express myfirstexpress # express的預設模版採用jade,若需要ejs模版支援,加上-e選項,即 express -e myfirstexpress$ cd myfirstexpress$ lsapp.js bin package.json public routes views #項目的目錄結構 

第三步、運行項目

複製代碼 代碼如下:

$ npm install #需要等待一段時間,因為需要擷取很多的庫檔案
$ npm start

第四步、訪問第一個express項目

瀏覽器中輸入 http://127.0.0.1:3000

第二種方法(文字說明和圖片相結合):

主要通過以下五步完成Nodejs的Express安裝

第一步:安裝express模組,cd到檔案夾中,使用npm install express命令安裝express module後,會發現檔案夾中多了node_modules目錄,裡邊會有express模組了。

第二步:進入到任意一個檔案夾,執行express app命令,就會建立一個app的應用項目,結構如下

E:\nodejs\express_demo>express app
   create : app
   create : app/package.json
   create : app/app.js
   create : app/public
   create : app/bin
   create : app/bin/www
   create : app/public/stylesheets
   create : app/public/stylesheets/style.css
   create : app/views
   create : app/views/index.jade
   create : app/views/layout.jade
   create : app/views/error.jade
   create : app/public/images
   create : app/routes
   create : app/routes/index.js
   create : app/routes/users.js
   create : app/public/javascripts
   install dependencies:  (這裡指示安裝必備的包)
     $ cd app && npm install
   run the app: (這裡指示執行,使用npm start)
     $ DEBUG=app ./bin/www

第三步:cd進入app檔案夾中,執行app,使用命令node app這時候會報錯,因為第一次使用express架構的話,缺少很多必備的modules

報錯:
module.js:340
    throw err;
          ^
Error: Cannot find module 'serve-favicon'//表示缺少serve-favicon模組
    at Function.Module._resolveFilename (module.js:338:15)
    at Function.Module._load (module.js:280:25)
    at Module.require (module.js:364:17)
    at require (module.js:380:17)
    at Object.<anonymous> (E:\nodejs\Node.js寮?鍙戝疄鎴榎chapter_two\express_dem
o\app\app.js:3:15)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)

第四步:這個時候根據提示,安裝必備的modules就可以了,如圖

Your environment has been set up for using Node.js 0.10.26 (ia32) and npm.
C:\Users\Administrator>e:
E:\>cd nodejs
E:\nodejs>npm install serve-favicon
serve-favicon@2.2.0 node_modules\serve-favicon
├── ms@0.7.0
├── parseurl@1.3.0
├── fresh@0.2.4
└── etag@1.5.1 (crc@3.2.1)
E:\nodejs>npm install morgan
morgan@1.5.1 node_modules\morgan
├── basic-auth@1.0.0
├── depd@1.0.0
├── debug@2.1.1 (ms@0.6.2)
└── on-finished@2.2.0 (ee-first@1.1.0)
E:\nodejs>npm install cookie-parser
cookie-parser@1.3.3 node_modules\cookie-parser
└── cookie-signature@1.0.5
E:\nodejs>npm install body-parser
body-parser@1.12.0 node_modules\body-parser
├── content-type@1.0.1
├── raw-body@1.3.3
├── bytes@1.0.0
├── depd@1.0.0
├── qs@2.3.3
├── iconv-lite@0.4.7
├── on-finished@2.2.0 (ee-first@1.1.0)
├── debug@2.1.1 (ms@0.6.2)
└── type-is@1.6.0 (media-typer@0.3.0, mime-types@2.0.9)
E:\nodejs>

第五步:安裝完成所有必備的modules後,在此執行app,項目根目錄下npm start命令,如果還是提示類似Error: Cannot find module 'jade'錯誤的話,繼續安裝必備的模組


第六步:以上所有的一切完成後,在瀏覽器下輸入http://127.0.0.1:3000/,如下圖就表示成功了。

 

以上是 windows下安裝nodejs及架構express的全部內容,希望大家喜歡。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.