我想按照下面的文件结构将我的rputes与main app.js
分开我已经尝试了api/user/
post api返回404,任何想法如何通过使用下面的文件结构解决这个问题?< / p>
./ app.js
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var db = require('./config/db');
var port = process.env.PORT || 8080;
app.use(require('./app/routes'));
app.use(bodyParser.json({ type: 'application/vnd.api+json' }));
app.use(bodyParser.urlencoded({ extended: true }));
app.use(methodOverride('X-HTTP-Method-Override'));
app.use(express.static(__dirname + '/public'));
function startServer() {
server.listen(port, function() {
console.log('Express server listening on %d, in %s mode', port, app.get('env'));
});
}
setImmediate(startServer);
// expose app
exports = module.exports = app;
./应用程序/ routes.js
module.exports = function(app) {
app.use('/api/user', require('./api/user'));
app.route('*')
.get(function(req, res) {
// res.sendFile('./public/views/index.html');
res.sendfile('./public/views/index.html');
});
};
./应用程序/ API /用户/ index.js
var express = require('express');
var controller = require('./user.controller');
var router = express.Router();
router.get('/', controller.index);
router.post('/',controller.create);
module.exports = router;
错误
angular.js: 12410 POST http: //localhost:8080/api/user 404 (Not Found)
(anonymous) @ angular.js: 12410
p @ angular.js: 12155(anonymous) @ angular.js: 11908(anonymous) @ angular.js: 16648
$eval @ angular.js: 17972
$digest @ angular.js: 17786
$apply @ angular.js: 18080(anonymous) @ angular.js: 26749
cg @ angular.js: 3613
d @ angular.js: 3601
angular.js: 14328 Possibly unhandled rejection: {
"data": "Cannot POST /api/user\n",
"status": 404,
"config": {
"method": "POST",
"transformRequest": [null],
"transformResponse": [null],
"jsonpCallbackParam": "callback",
"url": "/api/user",
"data": {
"firstName": "John",
"lastName": "Zhang"
},
"headers": {
"Accept": "application/json, text/plain, */*",
"Content-Type": "application/json;charset=utf-8"
}
},
"statusText": "Not Found"
}
答案 0 :(得分:2)
顺便说一句,您已在index.js
中定义了路线,并在routes.js
中加载,它会为404
提供/api/user
,因为它会在/
中搜索index.js
/api/users
,它只能找到/api/user/api/users
可以访问的user
。
您可以尝试将/api/user
路由器安装到/
,并可以在index.js
中定义var express = require('express');
var controller = require('./user.controller');
var router = express.Router();
//GET /api/user
router.get('/', controller.index);
//POST /api/user
router.post('/',controller.create);
module.exports = router;
路由
./ app / api / user / index.js
module.exports = function(app) {
//ANY /api/*
app.use('/api/user', require('./api/user'));
app.route('*')
.get(function(req, res) {
// res.sendFile('./public/views/index.html');
res.sendfile('./public/views/index.html');
});
};
<强> ./应用程序/ routes.js 强>
app.use(require('./app/routes'));
希望它对你有所帮助。
<强>更新强>
您的代码中还有其他问题。在 app.js 中,您定义了routes.js
,function(app)
为require('./app/routes')(app);
。
将该行更改为
app.use(express.static(__dirname + '/public'));
在var add_the_handlers = function (nodes) {
var i;
for (i = 0; i < nodes.length; i += 1) {
nodes[i].onclick = function (e) {
alert(i);
};
}
};
答案 1 :(得分:0)
您的问题与您的需求/导入有关。首先尝试使用app.js文件中的正常路径进行访问