这是我的问题.我没有在使用express的情况下在节点中写东西,所以我发现很难用基本API创建服务器.
基本上我在互联网上找到的是:
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(1337, '127.0.0.1');
console.log('Server running at http://127.0.0.1:1337/');
Run Code Online (Sandbox Code Playgroud)
但是我没有看到如何实现/index.html和/ getData.这段代码将在Raspberry派上运行,这就是我不应该使用库的原因.基本上我没有太多空间.
非常感谢,h
您需要手动检查请求中的URL并分别处理每个案例:
var http = require('http');
http.createServer(function (req, res) {
if(req.url == "/index.html") {
fs.readFile("index.html", function(err, text){
res.setHeader("Content-Type", "text/html");
res.end(text);
});
return;
}
if(req.url == "/getData") {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('get data\n');
return;
}
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(1337, '127.0.0.1');
console.log('Server running at http://127.0.0.1:1337/');
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2151 次 |
| 最近记录: |