“./ bin / www”是什么?在快递4.x? [英] What does "./bin/www" do in Express 4.x?

查看:145
本文介绍了“./ bin / www”是什么?在快递4.x?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始在我的Node.js应用程序中了解Express 4.0,我发现它生成了 ./ bin / www 文件,其中只有应用程序服务器和端口设置,其他所有其他类似中间件和路由的消息都定义在 ./ app.js 文件中。

I just started to learn about Express 4.0 in my Node.js app, and I found that it generated ./bin/www file, on which only the application server and port settings are written and everything others like middleware and routing is defined in ./app.js file.

但是,我不知道这个 ./ bin / www 是什么。我已经使用了Express 3.x,我一直在同样的 ./ app.js 文件中定义服务器和端口设置以及路由和中间件,并启动了我的节点应用程序与 node app.js 。那么使用 ./ bin / www 有什么意义?它只是将服务器和端口定义与其他人分开?

However, I'm not sure what this ./bin/www does. I've used Express 3.x and I have always defined server and port settings as well as routing and middleware on the identical ./app.js file, and launched my node app with node app.js. So what's the point of using the ./bin/www? Does it only separate the server and port definition from others?

现在,当我使用express-generator创建包时, package.json 包括以下定义:

Right now, when I create the package using express-generator, the package.json includes the following definition:

"scripts": {
    "start": "node ./bin/www"
}

但是,我想知道我是否应该启动应用程序使用节点./bin/www npm开始。我应该运行哪个命令启动我的应用程序?

However, I wonder whether I should launch my app using node ./bin/www, or npm start. Which command should I run to start my app?

而且,当我将应用程序部署到heroku时,应该在 Procfile 文件? web:node app.js 足够?

And also, when I deploy my app to heroku, what should I write in the Procfile file? Is web: node app.js enough?

推荐答案

Express 3.0 ,你通常会使用 app.configure()(或 app.use() )设置所需的中间件。您指定的那些中间件与Express 3.0捆绑在一起。

In Express 3.0, you normally would use app.configure() (or app.use() ) to set up the required middleware you need. Those middleware you specified are bundled together with Express 3.0.

示例:

var express = require('express');
var routes = require('./routes');
var user = require('./routes/user');
var http = require('http');
var path = require('path');

var app = express();

// all environments
app.set('port', process.env.PORT || 3000);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.compress());
app.use(express.json());
app.use(express.urlencoded());
app.use(express.methodOverride());

然而,在 Express 4.0 中,所有中间件都已被删除,保持和更新独立于核心Express(除了静态中间件),因此它们需要单独调用(您在 app.js 中看到的内容)。

In Express 4.0 however, all middleware have been removed so that they can be maintained and update independently from the core Express (except the static middleware), thus they need to be called separately (what you see in app.js).

bin / 目录用作可以定义各种启动脚本的位置, www 是一个关于它应该是如何的例子,最终你可以有启动脚本,如 test 停止重新启动等。使用此结构可以让您具有不同的配置,而无需触摸 app.js

The bin/ directory serve as a location where you can define your various startup scripts, the www is an example on how it should looks like, ultimately you could have startup script like test, stop or restart etc. Having this structure allows you to have different configurations without touching the app.js.

正确的方式开始您的Express应用程序是

The correct way to start you Express app is

npm start

要将 Express 4.x 应用程序部署到 Heroku ,将其添加到您的 Procfile 中。

To deploy an Express 4.x app to Heroku, add this to your Procfile.

web: npm start

这篇关于“./ bin / www”是什么?在快递4.x?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆