阅读本博客时候 请确保已经安装好了nodejs的npm
安装exress 命令:npm install -g express
然后可以使用express —help查看帮助信息
使用 express -X 查看下版本
好了 express 也安装完毕,我想我们需要再次了解下express是什么?
Express 是一个简洁、灵活的 node.js Web 应用开发框架, 它提供一系列强大的特性,帮助你创建各种 Web 和移动设备应用
Express 不对 node.js 已有的特性进行二次抽象,我们只是在它之上扩展了 Web 应用所需的基本功能。
更详细的学习资料建议大家可以查看如下网址:
http://www.expressjs.com.cn/guide.html
有一篇是总结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文件里面进行修改哈)
浏览器效果
你们看到的可能和我的不一致 我将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.