简体   繁体   English

启动Node.js Express应用程序

[英]Starting Node.js Express application

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. 我没有收到任何错误消息,终端只是转到下一行。 eg 例如

$ 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;

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

Your npm start probably calls your bin/www file. 你的npm start可能会调用你的bin/www文件。 Which contains the listen invocation to start your app. 其中包含启动应用程序的listen调用。

Many people set up their app this way. 许多人以这种方式设置他们的应用程序。 eg. 例如。 app.js to define and configure their app, and something like bin/www to actual get the server running. app.js定义和配置他们的应用程序,以及像bin/www这样的实际服务器运行。 This way they can include the app.js into other parts, say tests, without actually starting the server when you require it. 通过这种方式,他们可以将app.js包含在其他部分中,例如测试,而无需在require时实际启动服务器。

You have in your package.json file a property where you can point out the entry point of your application. 在package.json文件中有一个属性,您可以在其中指出应用程序的入口点。 For example: 例如:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM