Javascript 在express上创建MVC restful应用程序

Javascript 在express上创建MVC restful应用程序,javascript,node.js,rest,Javascript,Node.js,Rest,我正在尝试基于rails创建我自己的应用程序(不等同,但类似) 所以,我正在创建这个基本的东西发送到github,这样我就可以在任何项目中使用,我的路线有问题 我正在使用express资源创建cruds路由 这是我的应用程序 Controller/example.js: exports.index = function(req, res, next){ res.send('forum index'); next(); }; exports.new = function(req, res

我正在尝试基于rails创建我自己的应用程序(不等同,但类似)

所以,我正在创建这个基本的东西发送到github,这样我就可以在任何项目中使用,我的路线有问题

我正在使用express资源创建cruds路由

这是我的应用程序

Controller/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' });
  });
};
在my 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毫秒-- 错误:发送邮件后无法设置邮件头

我被困在这。。我不知道问题出在哪里。求求你,谁来帮忙!哈哈

非常感谢

res.send('forum index');
next();
响应请求或将请求传递给其他中间件进行响应,而不是两者都响应。

替换:

function(req, res, next)

并删除next()

接下来,不要停止请求

res.send('final output'); // end output
尝试:

这是另一种选择:

检查这篇文章(西班牙语)


Rails克隆

您不必调用
next()
而您已经调用了
send()
中间件,该中间件由
next()
调用,但无法调用,因为已作为
发送。send()
不会返回,并且您的进程会继续执行,只需删除
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' });
  });
};