簡體   English   中英

在Express上創建MVC穩定的應用程序

[英]Creating a MVC-restful application upon express

我正在嘗試基於Rails創建我自己的應用程序(不相等,但相似)。

所以,我正在創建要發送到github的基本內容,因此我可以在任何項目中使用,並且我的路線存在問題。

我正在使用express-resource來創建組隊路線。

這是我的應用程序。

控制器/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.

在終端中,發生以下錯誤:

GET / example / edit 304 1.080毫秒--錯誤:發送標頭后無法設置標頭。

我被困在這個..我不知道這個問題。 請,有人幫助! 哈哈

在此處輸入圖片說明

非常感謝!

res.send('forum index');
next();

響應或將請求傳遞給其他中間件以響應,而不是兩者都響應。

更換:

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/

您不必在已經調用send()的情況next()調用next()而由next()調用的中間件試圖調用send,但是不必這樣做,因為已經發送的.send()不會返回並且您的進程保持不變執行,只需刪除next()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM