“./bin/www"是什么意思?在 Express 4.x 中做什么? [英] What does "./bin/www" do in Express 4.x?

查看:56
本文介绍了“./bin/www"是什么意思?在 Express 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"
}

但是,我想知道我是否应该使用 node ./bin/wwwnpm start 启动我的应用程序.我应该运行哪个命令来启动我的应用程序?

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 updated independently from the core Express (except the static middleware), thus they need to be called separately (what you see in app.js).

bin/ 目录用作您可以定义各种启动脚本的位置.www 是将 express 应用程序作为 Web 服务器启动的示例.

The bin/ directory serves as a location where you can define your various startup scripts. The www is an example to start the express app as a web server.

最终,您可以拥有不同的脚本,例如 teststoprestart 等.拥有这种结构可以让您拥有不同的启动配置,无需将所有内容都塞进 app.js.

Ultimately, you could have different scripts like test, stop, or restart, etc. Having this structure allows you to have different startup configurations, without cramming everything into app.js.

启动 Express 应用的正确方法是:

The correct way to start your 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

或者如果你可以在你的 package.json 中使用启动脚本,heroku 会自动使用它,阅读更多 这里

Or if you can just use the start script in your package.json, heroku will automatically uses that, read more here

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

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

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