启动 Node.js Express 应用程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24471538/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Starting Node.js Express application
提问by Bryan Grace
I created an application with
我创建了一个应用程序
$ express -c stylus express_example
I can start my server with
我可以启动我的服务器
$ npm start
but
但
$ node app.js
&
&
$ nodemon app.js
don't seem to work. I don't get any error messages though, terminal just goes to the next line. e.g.
似乎不起作用。但是我没有收到任何错误消息,终端只是转到下一行。例如
$ node app.js
$
This is my bin/www file
这是我的 bin/www 文件
#!/usr/bin/env node
var debug = require('debug')('my-application');
var app = require('../app');
app.set('port', process.env.PORT || 3000);
var server = app.listen(app.get('port'), function() {
debug('Express server listening on port ' + server.address().port);
});
and my app.js file
和我的 app.js 文件
var express = require('express');
var path = require('path');
var favicon = require('static-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var routes = require('./routes/index');
var users = require('./routes/users');
var app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.use(favicon());
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded());
app.use(cookieParser());
app.use(require('stylus').middleware(path.join(__dirname, 'public')));
app.use(express.static(path.join(__dirname, 'public')));
app.use('/', routes);
app.use('/users', users);
/// catch 404 and forwarding to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
/// error handlers
// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: err
});
});
}
// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: {}
});
});
module.exports = app;
回答by Bryan Grace
Figured it out. Since my server is started in the bin/www file as opposed to the app.js file, from the terminal I went into my bin directory and then called
弄清楚了。由于我的服务器是在 bin/www 文件而不是 app.js 文件中启动的,因此我从终端进入我的 bin 目录,然后调用
$ node wwww
or
或者
$ nodemon www
回答by nowk
Your npm startprobably calls your bin/wwwfile. Which contains the listeninvocation to start your app.
您npm start可能调用了您的bin/www文件。其中包含listen启动应用程序的调用。
Many people set up their app this way. eg. app.jsto define and configure their app, and something like bin/wwwto actual get the server running. This way they can include the app.jsinto other parts, say tests, without actually starting the server when you requireit.
许多人以这种方式设置他们的应用程序。例如。app.js定义和配置他们的应用程序,以及bin/www实际让服务器运行。通过这种方式,他们可以将这些包含app.js到其他部分中,比如测试,而无需实际启动服务器require。
回答by Caio Penhalver
You have in your package.json file a property where you can point out the entry point of your application. For example:
您的 package.json 文件中有一个属性,您可以在其中指出应用程序的入口点。例如:
"scripts": {
"start": "node ./bin/www"
}

