我正在开发一个Node.js应用程序,使用http和httpdispatcher来处理Web服务器请求。我有以下代码:
var app = require('http').createServer(handler),
sys = require('util'),
exec = require('child_process').exec,
io = require('socket.io').listen(app),
fs = require('fs'),
dispatcher = require('httpdispatcher');
app.listen(61337);
dispatcher.setStatic('assets');
function puts(error, stdout, stderr) { sys.puts(stdout) }
function handler (request, response) {
dispatcher.dispatch(request, response);
}
dispatcher.onGet("/", function(req, res) {
res.writeHead(200, {'Content-Type': 'text/html'});
fs.readFile('./assets/index.html', 'utf8', function (err,data) {
res.end(data);
});
});
dispatcher.onGet("/api/update", function(req, res) {
exec("sh /path/to/update.sh &", puts);
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('1');
});
io.sockets.on('connection', function (socket) {
/** Socket.io coming soon */
});
我收到以下错误。
path.js:360
throw new TypeError('Arguments to path.join must be strings');
TypeError: Arguments to path.join must be strings
at path.js:360:15
at Array.filter (native)
at Object.exports.join (path.js:358:36)
at HttpDispatcher.staticListener (***/httpdispatcher.js:81:39)
at ***/httpdispatcher.js:132:3
at HttpChain.next (***/httpdispatcher.js:125:9)
at doDispatch (***/httpdispatcher.js:58:13)
at HttpDispatcher.dispatch (***/httpdispatcher.js:74:14)
at Server.handler (***/app-server.js:19:13)
at Server.<anonymous> (***/socket.io/node_modules/engine.io/lib/server.js:369:22)
Server.handler (***/app-server.js:19:13)
函数中的dispatcher.dispatch(request, response);
行是handler
..所以我不确定为什么它不发送字符串?
答案 0 :(得分:1)
我从使用httpdispatcher
转到express
以下是我的工作示例:
var express = require('express'),
app = express(),
http = require('http').createServer(app).listen(61337),
sys = require('util'),
exec = require('child_process').exec,
io = require('socket.io')(http),
fs = require('fs');
app.use('/assets', express.static('assets'));
function puts(error, stdout, stderr) { sys.puts(stdout) }
app.get("/", function(req, res) {
res.writeHead(200, {'Content-Type': 'text/html'});
fs.readFile('./assets/index.html', 'utf8', function (err,data) {
res.end(data);
});
});
app.get("/api/update", function(req, res) {
exec("sh /path/to/update.sh &", puts);
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('1');
});
io.sockets.on('connection', function (socket) {
});