nodejs中express框架

閱讀本博客時候 請確保已經安裝好了nodejs的npmhtml

安裝exress 命令:npm install -g expressnode

而後能夠使用express —help查看幫助信息shell

201506052226.jpg

使用 express -X 查看下版本express


201506052228.jpg

好了 express 也安裝完畢,我想咱們須要再次瞭解下express是什麼?npm

Express 是一個簡潔、靈活的 node.js Web 應用開發框架, 它提供一系列強大的特性,幫助你建立各類 Web 和移動設備應用
json

Express 不對 node.js 已有的特性進行二次抽象,咱們只是在它之上擴展了 Web 應用所需的基本功能。
瀏覽器

更詳細的學習資料建議你們能夠查看以下網址:bash

http://www.expressjs.com.cn/guide.html
app

有一篇是總結express 4.x以後的安裝,和部署 寫的很不錯 你們能夠參考下框架

http://www.cnblogs.com/Darren_code/p/express4.html

 

OK了 一切都弄好了 如今咱們開始使用

在shell中執行 npm -e myapp2 (注意:-e是表示使用ejs模板 默認使用的是jade模塊)

cd myapp2 進入目錄

而後執行 npm install (根據package.json裏面的內容進行此工程相關的包的安裝)

安裝完畢後執行 npm start

這個時候咱們在瀏覽器裏面輸入http://localhost:3000 (默認監聽的端口是3000 你能夠在bin文件裏面進行修改哈)


201506060844.jpg

瀏覽器效果


201506060842.jpg

大家看到的可能和個人不一致 我將index.ejs作了一點小小修改哈!

今天在另一臺電腦從新安裝express 安裝完畢後 執行express 報錯

-bash: express: command not found

後查找資料發現 在4.0以後 安裝express須要這樣安裝

npm install -g express-generator

 

在使用supervisor 中出現錯誤:

Supervisor node .js 「Program node app exited with code 0」 error

解決方案以下:

The app that the generator creates calls ./bin/www that includes app.js and then starts listening for traffic.

app.js does not do this itself.

I think this is important to understand.

app.listen is not being called in app.js but is called in ./bin/www...and this is why you get the exit 0 result. When you call app.js and not ./bin/www it runs through the file but because is no command to listen for traffic, the program ends normally...i.e. without having done anything.

That said, you have two options..

Option 1

If you have a ./bin/www file, you could run supervisor ./bin/www to get things started.

Option 2

If you don't have the ./bin/www file for whatever reason, you can edit your app file to look like this.

In your app listing, replace

module.exports = app;

with this

app.set('port', process.env.PORT || 3000); var server = app.listen(app.get('port'), function() { debug('Express server listening on port ' + server.address().port); });

Important Note

While that edit will start the app listening and you won't get an exit 0 any more, I cannot guarantee that the app won't crash with some other error if other files and directories are missing. For example, if the routes directory isn't present, then the declarations requiring routes/index and routes/users will fail and other bad things will happen.

相關文章
相關標籤/搜索