我是nodejs的新手,我想从app / routes.js处理所有路由和api,我有路由器,我用来获取和发布客户端。我没有看到服务器与客户端之间的任何响应,代码如下,任何想法?
app.js
var express = require('express');
var app = express();
var server = require('http').createServer(app);
var io = require('./app/io').initialize(server);
var ditconsumer = require('./app/consumers/ditconsumer');
ditconsumer.start(server);
server.listen(3000, function () {
console.log('Example app listening on port 3000!');
//stconsumer.start();
});
应用程序/ routes.js
var express = require('express');
var router = express.Router();
var dirDirectory = require('./app/serverfiles/ditDir');
module.exports = function(app){
router.get('/getAllFiles',function(req,res){
dirDirectory.readDirectory(function(logFiles){
res.json(logFiles);
console.log(logFiles);
});
});
//other routes..
}
答案 0 :(得分:2)
所以这就是你做错了
这是您更改的代码,请尝试一下,我知道它是否有帮助
var express = require('express');
var app = express();
var myroute=require('./app/routes.js');
app.use(myroute);
var server = require('http').createServer(app);
var io = require('./app/io').initialize(server);
var ditconsumer = require('./app/consumers/ditconsumer');
ditconsumer.start(server);
server.listen(3000, function () {
console.log('Example app listening on port 3000!');
//stconsumer.start();
});
应用程序/ routes.js
var express = require('express');
var router = express.Router();
var dirDirectory = require('./app/serverfiles/ditDir');
router.get('/getAllFiles',function(req,res){
dirDirectory.readDirectory(function(logFiles){
res.json(logFiles);
console.log(logFiles);
});
});
module.exports=router;
//other routes..
}
如果这可以解决您的问题,请告诉我