首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >基于express创建MVC-restful应用程序

基于express创建MVC-restful应用程序
EN

Stack Overflow用户
提问于 2014-12-29 00:33:33
回答 3查看 215关注 0票数 0

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

所以,我创建了这个基本的东西发送到github,所以我可以在任何项目中使用,但我的路由有问题。

我正在使用express-resource创建cruds路由。

这是我的应用。

Controller/example.js:

代码语言:javascript
运行
AI代码解释
复制
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中:

代码语言:javascript
运行
AI代码解释
复制
var express     = require('express');
var resource    = require('express-resource');

var client = express();

routes.resource('example', require('../controllers/example'));


module.exports = routes;

和我的app.js:

代码语言:javascript
运行
AI代码解释
复制
// Routes
var routes = require('./routes/routes.js');

app.use('/', routes);

现在的问题是:

我只能访问索引和新路由。当我尝试访问时:

代码语言:javascript
运行
AI代码解释
复制
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.

在终端中,出现以下错误:

GET /example/edit 304 1.080毫秒--错误:发送后无法设置标头。

我被困在这里了..。我不知道问题出在哪里。快来人救救我!哈哈

非常感谢!

EN

回答 3

Stack Overflow用户

发布于 2014-12-29 03:12:44

代码语言:javascript
运行
AI代码解释
复制
res.send('forum index');
next();

要么响应,要么传递请求,让其他中间件响应,而不是两者都响应。

票数 0
EN

Stack Overflow用户

发布于 2014-12-29 03:56:59

替换:

代码语言:javascript
运行
AI代码解释
复制
function(req, res, next)

使用

代码语言:javascript
运行
AI代码解释
复制
function(req, res)

并删除下一步();

下一步,不要停止请求。

代码语言:javascript
运行
AI代码解释
复制
res.send('final output'); // end output

尝试:

代码语言:javascript
运行
AI代码解释
复制
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/

票数 0
EN

Stack Overflow用户

发布于 2014-12-30 10:22:09

您不必在已经调用send()时调用next()next()试图调用send但无法调用的中间件,因为已经作为.send()发送的消息没有返回,并且您的进程继续执行,只需删除next()即可

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27682054

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档