I am following a tutorial on Node.js and I think I have done everything to the letter, I run my server (node web.js) and tried to connect to it but I am getting an error. My code is given below, I saw an answer to a similar question but I avoided the error there, I just don't know whats wrong. Please help!
var http = require("http");
function process_request(req, res) {
var body = 'Thanks for calling!\n';
var content_length = body.length;
res.writeHead(200, {
'Content-Length': content_length,
'Content-Type': 'text/plain'
});
}
var s = http.createServer(process_request);
s.listen(8080);
You need to write the content that you are sending back as a response. Add this after your res.writeHead()
.
res.write(body);
res.end();
You didn't provide the error, but you could try a
res.send(body);
Instead. Also confirm you're hitting http://localhost : 8080 / (assuming it's running on localhost).
You are creating content but not sending to client , using res.end you can do like
res.write("Hello World");
res.end(body)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.