我正在尝试基于rails创建我自己的应用程序(不相同,但相似)。
所以,我正在创建这个基本的东西发送到github,所以我可以在任何项目中使用,我的路线有问题。
我正在使用express-resource来创建cruds路由。
这是我的应用程序。
控制器/ example.js:
exports.index = function(req, res, next){
res.send('forum index');
next();
};
exports.new = function(req, res, next){
res.send('new forum');
next();
};
exports.create = function(req, res, next){
res.send('create forum');
next();
};
exports.show = function(req, res, next){
res.send('show forum');
next();
};
exports.edit = function(req, res, next){
res.send('edit forum');
next();
};
exports.update = function(req, res, next){
res.send('update forum');
next();
};
exports.destroy = function(req, res, next){
res.send('destroy forum');
next();
};
exports.load = function(id, fn){
process.nextTick(function(){
fn(null, { title: 'Ferrets' });
});
};
我的routes.js:
var express = require('express');
var resource = require('express-resource');
var client = express();
routes.resource('example', require('../controllers/example'));
module.exports = routes;
和我的app.js:
// Routes
var routes = require('./routes/routes.js');
app.use('/', routes);
现在出现问题:
我只能访问索引和新路由。当我尝试访问时:
http://localhost:3000/example - will show right, but with a 304 http code.
http://localhost:3000/example/new - will show right, but with a 304 http code.
http://localhost:3000/example/create - will show the /show/ and a 304 http code.
http://localhost:3000/example/show - will show the /show/ and a 304 http code.
http://localhost:3000/example/edit - will show the /show/ and a 304 http code.
http://localhost:3000/example/update - will show the /show/ and a 304 http code.
http://localhost:3000/example/destroy - will show the /show/ and a 304 http code.
在终端中,发生以下错误:
获取/示例/编辑304 1.080毫秒 - - 错误:发送后无法设置标头。
我陷入了困境......我不知道这个问题。请有人帮忙!哈哈
非常感谢!
答案 0 :(得分:0)
res.send('forum index');
next();
响应或传递请求以便其他中间件响应,而不是两者。
答案 1 :(得分:0)
替换:
function(req, res, next)
与
function(req, res)
然后移除 next();
接下来不要停止请求。
res.send('final output'); // end output
尝试:
exports.index = function(req, res){
res.send('forum index');
};
exports.new = function(req, res){
res.send('new forum');
};
exports.create = function(req, res){
res.send('create forum');
};
exports.show = function(req, res){
res.send('show forum');
};
exports.edit = function(req, res){
res.send('edit forum');
};
exports.update = function(req, res){
res.send('update forum');
};
exports.destroy = function(req, res){
res.send('destroy forum');
};
exports.load = function(id, fn){
process.nextTick(function(){
fn(null, { title: 'Ferrets' });
});
};
这是另一种选择:https://www.npmjs.com/package/express-enrouten
查看此文章(西班牙语)http://blog.mcnallydevelopers.com/cargar-controladores-de-forma-automatica-en-expressjs-con-node-js/
Rails克隆? http://sailsjs.org/
答案 2 :(得分:0)
您已经调用了next()
send()
,而next()
调用的中间件尝试呼叫发送但是因为已经发送{{1}而无法呼叫.send()
}}不会返回,您的流程会继续执行,只需删除next()