Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/40.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 为什么Express.js的app.get()只能在调用app.listen()的同一文件中工作?_Node.js_Express - Fatal编程技术网

Node.js 为什么Express.js的app.get()只能在调用app.listen()的同一文件中工作?

Node.js 为什么Express.js的app.get()只能在调用app.listen()的同一文件中工作?,node.js,express,Node.js,Express,当app.listen与app.get位于同一个文件中时,它可以工作;当我通过require在其他文件中添加app.get调用时,它们不起作用: // ~ means root folder // This below is in ~/index.js var routes = require('~/routes/routes.js'); var server = app.listen(3000, function () { console.log('Listening on port

当app.listen与app.get位于同一个文件中时,它可以工作;当我通过require在其他文件中添加app.get调用时,它们不起作用:

// ~ means root folder
// This below is in ~/index.js
var routes = require('~/routes/routes.js');

var server = app.listen(3000, function () {
    console.log('Listening on port %d', server.address().port);
});

app.get('/snails', function (req, res) {
    res.send('ESCARGOT');
});

// This below is in ~/routes/routes.js
var app = module.exports = require('exports')();

app.get('/birds', function () {
    res.send('PENGUIN');
});

// SUCCESS -> localhost:3000/snails will return "ESCARGOT"
// FAIL -> localhost:3000/birds will return "Cannot GET /birds"
第二个例子来证明这一点;这一次,app.listen被移动到routes.js:

// ~ means root folder
// The below is in ~/index.js
var routes = require('~/routes/routes.js');

app.get('/snails', function (req, res) {
    res.send('ESCARGOT');
});

// The below is in ~/routes/routes.js
var app = module.exports = require('exports')();

app.get('/birds', function () {
    res.send('PENGUIN');
});

var server = app.listen(3000, function () {
    console.log('Listening on port %d', server.address().port);
});

// FAIL -> localhost:3000/snails will return "Cannot GET /snails"
// SUCCESS -> localhost:3000/birds will return "PENGUIN"

为什么会这样?是因为app.listen只针对调用它的文件吗?

您需要导出应用程序并将其包含在路由文件中

module.exports = app;
var app = include('pathtoyourapp.js');
然后在你的路由文件中

module.exports = app;
var app = include('pathtoyourapp.js');

然后,您可以访问routes文件中的应用程序。

您应该在routes/routes.js中按照这一思路进行操作

在index.js中

var app = express();
app.get('/snails', function(req, res, next) {
   res.send('SNAILS');
});
require('./routes/routes')(app);

app.listen(3000);
现在应该可以了

顺便说一句,我不是100%确定你要做什么需要“导出”,而且看起来很奇怪,你实际上是在导出它,而不是在routes/routes.js中包含新的鸟类路线的应用程序,所以这就是为什么它可能不起作用的原因。试试我建议的方法

如果您还需要其他东西,请告诉我。

使用示例:

var express = require('express'),
    http = require('http'),
    port = Number(process.env.PORT || 3000),
    app = express();

app.get('/', function(req, res) {
    res.end('Test message');
});

http.createServer(app).listen(port);
最重要的是:

http.createServer(app).listen(port);

发送应用程序参数以操纵服务器行为。

是否包括routes/routes.js?它可以访问应用程序吗?编辑上面的代码以包含它们我不确定为什么这会起作用,因为您没有将res添加为路由回调参数之一。