Javascript Node.js控制器
我目前对Node.js-Express框架非常陌生,我正在尝试模拟MVC模式,到目前为止,我了解到了这一点 app.jsJavascript Node.js控制器,javascript,node.js,express,Javascript,Node.js,Express,我目前对Node.js-Express框架非常陌生,我正在尝试模拟MVC模式,到目前为止,我了解到了这一点 app.js var express = require('express'); var path = require('path'); var favicon = require('static-favicon'); var logger = require('morgan'); var cookieParser = require('cookie-parser'); var bodyP
var express = require('express');
var path = require('path');
var favicon = require('static-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var http = require('http');
var app = module.exports = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.set('port', process.env.PORT || 3000);
app.use(favicon());
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded());
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
require('./routes');
/// catch 404 and forwarding to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
/// error handlers
// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: err
});
});
}
// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: {}
});
});
http.createServer(app).listen(app.get('port'), function() {
console.log("Running server");
});
只是使用express generator生成的一个普通app.js,经过一些修改,现在是my routes/index.js
app = require('../app');
app.get('/test', function(req, res) {
require('./controllers/test.js');
test();
});
app.get('/', function(req, res) {
res.send('yankevf');
});
正如您在route/test上看到的,if查找模块/controllers/test.js,但由于没有实际的模块,因此无法工作
如何包含文件夹控制器中的文件?因为我的服务器一直在节点模块内搜索,所以看起来你基本上就在那里了 从您的问题听起来,您的文件夹结构是:
./app.js
./routes
-> index.js
./controllers
-> test.js
如果是这样的话,这应该是可行的:
app = require('../app');
app.get('/test', function(req, res, next) {
test = require('../controllers/test.js'); // Note the path, one directory up
// Passing the req, res, next arguments on to your controller allows it to respond
// i.e. res.json({msg: 'hello world'}).
test(req, res, next);
});
虽然您可能不希望在每个新请求中加载控制器,但希望加载(要求)它一次,并在每个请求中调用它:
app = require('../app');
testController = require('../controllers/test.js');
app.get('/test', function(req, res, next) {
// Passing the req, res, next arguments on to your controller allows it to respond
// i.e. res.json({msg: 'hello world'}).
testController(req, res, next);
});
如果您直接将这些参数传递给控制器(就像您应该传递的那样),则可以更简洁地编写:
app = require('../app');
testController = require('../controllers/test.js');
// Call your controller, automatically passing 'req', 'res', 'next' arguments.
app.get('/test', testController);
有关自动装载路由的完整示例,请签出:看起来您基本上就在那里了 从您的问题听起来,您的文件夹结构是:
./app.js
./routes
-> index.js
./controllers
-> test.js
如果是这样的话,这应该是可行的:
app = require('../app');
app.get('/test', function(req, res, next) {
test = require('../controllers/test.js'); // Note the path, one directory up
// Passing the req, res, next arguments on to your controller allows it to respond
// i.e. res.json({msg: 'hello world'}).
test(req, res, next);
});
虽然您可能不希望在每个新请求中加载控制器,但希望加载(要求)它一次,并在每个请求中调用它:
app = require('../app');
testController = require('../controllers/test.js');
app.get('/test', function(req, res, next) {
// Passing the req, res, next arguments on to your controller allows it to respond
// i.e. res.json({msg: 'hello world'}).
testController(req, res, next);
});
如果您直接将这些参数传递给控制器(就像您应该传递的那样),则可以更简洁地编写:
app = require('../app');
testController = require('../controllers/test.js');
// Call your controller, automatically passing 'req', 'res', 'next' arguments.
app.get('/test', testController);
对于自动装载路由的完整示例,请检查:不知道您在问什么“我正在尝试枚举一个MVC模式”-天哪,为什么?我的确切感觉…@DalorzoWell,来自使用PHP和MVC模式真的很有帮助,那么我应该如何使用node-express?路线内的一切?。如何包含文件夹控制器中的文件?由于我的服务器一直在节点模块内搜索,我不知道您在PHP中做了什么,但这不是MVC模式。MVC需要视图来观察模型和无状态协议,这几乎是不可能的,除非你一直使用AJAX(或一直使用WebSocket)。不知道你在问什么“我试图列举一个MVC模式”-天哪,为什么?我确切的感觉是…@DalorzoWell,来自使用PHP和MVC模式真的很有帮助,那么我应该如何使用node-express呢?路线内的一切?。如何包含文件夹控制器中的文件?由于我的服务器一直在节点模块内搜索,我不知道您在PHP中做了什么,但这不是MVC模式。MVC需要视图来观察模型和无状态协议,除非您一直使用AJAX(或一直使用WebSocket),否则这几乎是不可能的。如何将res和req对象发送到控制器?因此,我可以在controller.jsI上使用res.send来试图澄清问题。github上的express存储库中也有很好的示例应用程序。有些是为了演示如何在MVC风格的后端中使用express而编写的。如何将res和req对象发送到控制器?因此,我可以在controller.jsI上使用res.send来试图澄清问题。github上的express存储库中也有很好的示例应用程序。有些是为了展示如何在MVC风格的后端中使用express而编写的。