Đây là những gì tôi làm:
tôi có một server.js tập tin bên trong thư mục gốc của dự án nút của tôi rằng thiết lập ứng dụng nút máy chủ lication (với tốc) và kim ngạch xuất khẩu 2 phương pháp:
exports.start = function(config, readyCallback) {
if(!this.server) {
this.server = app.listen(config.port, function() {
console.log('Server running on port %d in %s mode', config.port, app.settings.env);
// callback to call when the server is ready
if(readyCallback) {
readyCallback();
}
});
}
};
exports.close = function() {
this.server.close();
};
Các app.js tập tin sẽ được đơn giản vào thời điểm này:
var server = require('./server');
server.start({ port: 8000 });
Vì vậy, các tập tin/thư mục cấu trúc cơ bản sẽ được như sau :
src
app.js
server.js
Có tách này sẽ cho phép bạn chạy các máy chủ thường:
node src/app.js
.. và/hoặc yêu cầu nó từ một kịch bản nút tùy chỉnh, mà có thể là một kịch bản nút (hoặc một jake/grunt/bất cứ công việc), được thực hiện các xét nghiệm của bạn như thế này:
/** my-test-task.js */
// util that spawns a child process
var spawn = require('child_process').spawn;
// reference to our node application server
var server = require('./path/to/server.js');
// starts the server
server.start({ port: 8000 }, function() {
// on server ready launch the jasmine-node process with your test file
var jasmineNode = spawn('jasmine-node', [ '.path/to/test/file.js' ]);
// logs process stdout/stderr to the console
function logToConsole(data) {
console.log(String(data));
}
jasmineNode.stdout.on('data', logToConsole);
jasmineNode.stderr.on('data', logToConsole);
jasmineNode.on('exit', function(exitCode) {
// when jasmine-node is done, shuts down the application server
server.close();
}
});
Bạn đang chạy phiên bản Express nào? – srquinn
Tôi đang sử dụng phiên bản 3.x (3.0.2 ngay bây giờ). – optikfluffel