Following two app.js both run fine on my local computer. But when I upload them to Tommy, the first script went wrong while the second is OK. So I wonder what could cause this? They seem to do the same thing.
(1) app.js using net
var net = require("net");
var server = net.createServer(function (socket) {
socket.on("data", function (data) {
let resp = "HTTP/1.0 200 OK\r\n" +
"Content-Type: text/html\r\n" +
"Content-Length: 10\r\n" +
"\r\nIt works!!";
socket.write(resp);
});
});
server.listen(3000, '127.0.0.1', function () {
console.log("listening on port 3000");
});
(2) app.js using http
const http = require('http');
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/html');
res.end(`Node.js works!`);
});
server.listen(3000, '127.0.0.1', () => {
console.log("listening on port 3000");
});