这是我的代码:
var http = require('http');
function onRequest(req, res) {
// do something
}
var server = http.createServer(onRequest);
server.listen(8000);
console.log("The server is now running on " + <<server.address>>);
我希望 <> 类似于“http://localhost:8000”。我该怎么做?
最佳答案
var http = require('http');
var server = http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(8080);
console.log('Server listening:', `http://${server.address().address}:${server.address().port}`);
我觉得你可以用
server.address()#
Added in: v0.1.90 Returns the bound address, the address family name, and port of the server as reported by the operating system. Useful to find which port was assigned when getting an OS-assigned address. Returns an object with port, family, and address properties: { port: 12346, family: 'IPv4', address: '127.0.0.1' }
关于node.js - 你如何在 node.js 中打印 http 服务器的地址?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41540093/