Mac下NodeJS學習筆記 搭建第一個NodeJS開發環境

來源:互聯網
上載者:User

標籤:nodejs 環境搭建

一.準備

     已安裝NodeJS(Node,npm)

二.

     1.建立工作空間: 

$ mkdir ~/workspace/nodejs/$ cd ~/workspace/nodejs/$ pwd/Users/vincent/Desktop/my/workspace/nodejs

     2.安裝  express

$ sudo npm install express -g[email protected] /usr/local/lib/node_modules/express├── [email protected]├── [email protected]├── [email protected]├── [email protected]├── [email protected]├── [email protected]├── [email protected]├── [email protected]├── [email protected]├── [email protected]├── [email protected]├── [email protected]├── [email protected]├── [email protected]├── [email protected]├── [email protected]├── [email protected]├── [email protected] ([email protected])├── [email protected] ([email protected])├── [email protected] ([email protected])├── [email protected] ([email protected], [email protected])├── [email protected] ([email protected], [email protected], [email protected])├── [email protected] ([email protected])└── [email protected] ([email protected], [email protected])

   

   3.建立express工程,啟動第一個項目

     

$ express -e nodejs-demo-bash: express: command not found

   我在此處也被坑過,找度娘查了一下,從結果中找出了一條比較滿意的結果:

  • Express Command not found

  • 最初操作:跟著《nodejs開發指南》敲npm install -g express,安裝好了,就在linux命令列敲express --help就出現上面的原因了。

  • 解決方案:在安裝一個包npm install -g express-generator

  • 原因:express3+已經把建立一個APP的功能分離出來為express-generator,沒它你建立不了應用程式

  • 2 express -t ejs microblog建立的不是ejs模板引擎而是jade模板引擎

  • 最初操作:跟著《nodejs開發指南》在命令列敲express -t ejs microblog是用來建立應用程式的,應用程式的名字叫microblog、模板引擎是ejs

  • 出現情況:但是通過查看microblog檔案夾中的package.json中知道建立出來的不是ejs模板而是jade模板引擎

  • 解決方案:版本不一樣,用錯命令了,應該是express -e microblog(-e就是ejs模板)

  • 3 node app.js沒效果

  • 最初操作:跟著《nodejs開發指南》在命令列敲node app.js,然後就用瀏覽器訪問監聽的3000連接埠

  • 出現情況:訪問不到頁面

  • 解決方案:版本不一樣,用錯命令了,應該是npm start

    上面這段從 http://blog.csdn.net/dai_jing/article/details/38087443 處擷取。   


   找到原因之後,執行如下命令:

$ sudo npm install -g express-generator/usr/local/bin/express -> /usr/local/lib/node_modules/express-generator/bin/express[email protected] /usr/local/lib/node_modules/express-generator├── [email protected] ([email protected])└── [email protected] ([email protected])
$ express -e nodejs-demo   create : nodejs-demo   create : nodejs-demo/package.json   create : nodejs-demo/app.js   create : nodejs-demo/public   create : nodejs-demo/public/images   create : nodejs-demo/public/javascripts   create : nodejs-demo/public/stylesheets   create : nodejs-demo/public/stylesheets/style.css   create : nodejs-demo/routes   create : nodejs-demo/routes/index.js   create : nodejs-demo/routes/users.js   create : nodejs-demo/views   create : nodejs-demo/views/index.ejs   create : nodejs-demo/views/error.ejs   create : nodejs-demo/bin   create : nodejs-demo/bin/www   install dependencies:     $ cd nodejs-demo && npm install   run the app:     $ DEBUG=nodejs-demo ./bin/www

     安裝依賴包

$ cd nodejs-demo/$ sudo npm install[email protected] node_modules/ejs[email protected] node_modules/cookie-parser├── [email protected]└── [email protected][email protected] node_modules/debug└── [email protected][email protected] node_modules/serve-favicon├── [email protected]├── [email protected]└── [email protected] ([email protected])[email protected] node_modules/morgan├── [email protected]├── [email protected]└── [email protected] ([email protected])[email protected] node_modules/express├── [email protected]├── [email protected]├── [email protected]├── [email protected]├── [email protected]├── [email protected]├── [email protected]├── [email protected]├── [email protected]├── [email protected]├── [email protected]├── [email protected]├── [email protected]├── [email protected]├── [email protected]├── [email protected] ([email protected])├── [email protected]├── [email protected] ([email protected])├── [email protected] ([email protected], [email protected])├── [email protected] ([email protected], [email protected], [email protected], [email protected])├── [email protected] ([email protected], [email protected])└── [email protected] ([email protected])[email protected] node_modules/body-parser├── [email protected]├── [email protected]├── [email protected]├── [email protected]├── [email protected] ([email protected])├── [email protected]├── [email protected]└── [email protected] ([email protected])

啟動程式

$ npm start> [email protected] start /Users/vincent/Desktop/my/workspace/nodejs/nodejs-demo> node ./bin/www

另外再開啟一個終端測試是否啟動成功

$ curl localhost:3000<!DOCTYPE html><html>  <head>    <title>Express</title>    <link rel=‘stylesheet‘ href=‘/stylesheets/style.css‘ />  </head>  <body>    <h1>Express</h1>    <p>Welcome to Express</p>  </body></html>


本文內容是根據http://blog.fens.me/nodejs-enviroment/   中的說明進行的操作記錄。

該連結來自於:http://blog.fens.me/series-nodejs/

這個連結中的內容很適合NodeJS新手。


本文出自 “奮進的小鳥” 部落格,請務必保留此出處http://vincent90.blog.51cto.com/3221926/1584006

Mac下NodeJS學習筆記 搭建第一個NodeJS開發環境

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.