Ove*_*d D 28
首先,您需要将实际的应用程序设置移动到一个模块中,然后将其导入到实际启动应用程序的文件中.现在这是分开的,您可以在实际收听之前让应用程序处于完整状态.
您应该将应用程序的实际设置移动到一个单独的文件中,让我们称之为app.js,可以从您运行节点的文件中调用listen,让我们称之为index.js.
所以,app.js看起来像:
var express = require('express')
, routes = require('./routes');
var app = module.exports = express.createServer();
// Configuration
app.configure(function(){
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(__dirname + '/public'));
});
app.configure('development', function(){
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});
app.configure('production', function(){
app.use(express.errorHandler());
});
// Routes
app.get('/', routes.index);
Run Code Online (Sandbox Code Playgroud)
和index.js看起来像:
var app = require('./app');
app.listen(3000, function(){
console.log("Express server listening on port %d in %s mode", app.address().port, app.settings.env);
});
Run Code Online (Sandbox Code Playgroud)
这可以分离您的应用程序的加载,实际上让它听,允许您将该应用程序加载到您的单元测试中.
在单元测试中,您将在设置方法和拆解方法中执行某些操作来启动和关闭服务器.
在文件test/app_tests.js中:
describe('app', function(){
var app = require('../app');
beforeEach(function(){
app.listen(3000);
});
// tests here
afterEach(function(){
app.close();
})
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8594 次 |
| 最近记录: |