Node.js 服务器无法启动

标签 node.js server

我正在尝试使用命令“node server.js”运行这个极其简单的服务器示例。这给出了服务器正在运行的消息,但是,当我在 Chrome 中访问 127.0.0.1:3000 或 localhost:3000 时,它看起来像是被卡住了(渲染图标连续旋转)。

代码:

var http = require('http');
var server = http.createServer();
server.on('request',function(req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.write('Hello World \n');
})

server.listen(3000);
console.log('Server running on localhost 3000');

罗德里戈回答正确。这是从(node.js 运行中,以防您遇到相同问题)中提取的

最佳答案

在写入要发送的数据后放置res.end()。它应该可以工作。

var http = require('http');
var server = http.createServer();
server.on('request',function(req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.write('Hello World \n');
  res.end()
})

server.listen(3000);
console.log('Server running on localhost 3000');

关于Node.js 服务器无法启动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30458030/

相关文章:

C : non blocking sockets with timeout : how to avoid busy waiting?

node.js - 通过双击 bash 文件在 Ubuntu 中运行 bash 脚本(其中包含一些 npm 命令)会遇到 `npm: command not found` 错误

node.js - 如何将 nodeJS 集群与 mySQL 池集群一起使用?

javascript - MongoDB:如何将数组的 $size 与另一个文档项进行比较?

javascript - 你能强制 Vue.js 重新加载/重新渲染吗?

Java 客户端/服务器应用程序不会 readLine()

基于 python 的 https 服务器在提供响应后关闭连接

node.js - 如何将 nodejs 模块安装到 appfog

外部IP访问Tomcat

ios - OS X/iOS下如何使用Security.framework实现服务器端服务器名称指示