nodejs中express框架

阅读本博客时候 请确保已经安装好了nodejs的npm

安装exress 命令:npm install -g express

然后可以使用express —help查看帮助信息

201506052226.jpg

使用 express -X 查看下版本


201506052228.jpg

好了 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文件里面进行修改哈)


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.

 

转载于:https://www.cnblogs.com/greenboy/p/4556168.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值