Node.js http://localhost:8080/ 它不起作用了
我对nodeJS和triyng都是新手,我想学习它。Node.js http://localhost:8080/ 它不起作用了,node.js,Node.js,我对nodeJS和triyng都是新手,我想学习它。 我正在尝试从中执行hello world示例 但我并没有得到任何输出,在chrome浏览器的页面上也并没有收到任何数据。 我已经在我的电脑上安装了apache(XAMPP),但它不是活动的,而且当我试图在终端中运行节点http.js时,我没有得到任何输出。 我还有一个文件hello.js,其中包含console.log('hello World!') 当我运行了节点hello.js后,我得到了hello World终端中的输出。 但是ht
我正在尝试从中执行hello world示例 但我并没有得到任何输出,在chrome浏览器的页面上也并没有收到任何数据。
我已经在我的电脑上安装了apache(XAMPP),但它不是活动的,而且当我试图在终端中运行
节点http.js
时,我没有得到任何输出。
我还有一个文件hello.js,其中包含
console.log('hello World!')代码>
当我运行了节点hello.js
后,我得到了hello World代码>终端中的输出。
但是http.js
不起作用。
http.js代码:
// Include http module.
var http = require("http");
// Create the server. Function passed as parameter is called on every request made.
// request variable holds all request parameters
// response variable allows you to do anything with response sent to the client.
http.createServer(function (request, response) {
// Attach listener on end event.
// This event is called when client sent all data and is waiting for response.
request.on("end", function () {
// Write headers to the response.
// 200 is HTTP status code (this one means success)
// Second parameter holds header fields in object
// We are sending plain text, so Content-Type should be text/plain
response.writeHead(200, {
'Content-Type': 'text/plain'
});
// Send data and end response.
response.end('Hello HTTP!');
});
// Listen on the 8080 port.
}).listen(8080);
我想您使用节点0.10.x或更高版本?它在stream
api(通常称为Streams2)中有一些更改。Streams2中的一个新特性是,在您完全使用流之前(即使流为空),不会触发end
事件
如果您确实希望在end
事件上发送请求,则可以使用Streams 2 API使用流:
var http = require('http');
http.createServer(function (request, response) {
request.on('readable', function () {
request.read(); // throw away the data
});
request.on('end', function () {
response.writeHead(200, {
'Content-Type': 'text/plain'
});
response.end('Hello HTTP!');
});
}).listen(8080);
或者您可以将流切换到旧(流动)模式:
否则,您可以立即发送响应:
var http = require('http');
http.createServer(function (request, response) {
response.writeHead(200, {
'Content-Type': 'text/plain'
});
response.end('Hello HTTP!');
}).listen(8080);
试试这个
//Lets require/import the HTTP module
var http = require('http');
//Lets define a port we want to listen to
const PORT=8080;
//We need a function which handles requests and send response
function handleRequest(request, response){
response.end('It Works!! Path Hit: ' + request.url);
}
//Create a server
var server = http.createServer(handleRequest);
//Lets start our server
server.listen(PORT, function(){
//Callback triggered when server is successfully listening. Hurray!
console.log("Server listening on: http://localhost:%s", PORT);
});
控制台中是否有任何错误?关于发生了什么有什么提示吗?当我在浏览器中转到localhost:8080时,这段代码对我来说效果很好。你试过在浏览器中键入:吗?在我的浏览器中,两者都适用于我。。但您确实需要在url中指定8080端口。控制台中没有错误。我在这里没有使用代理。哇!你知道为什么会这样吗?他们网站上的NodeJS示例不会为我加载任何数据。
//Lets require/import the HTTP module
var http = require('http');
//Lets define a port we want to listen to
const PORT=8080;
//We need a function which handles requests and send response
function handleRequest(request, response){
response.end('It Works!! Path Hit: ' + request.url);
}
//Create a server
var server = http.createServer(handleRequest);
//Lets start our server
server.listen(PORT, function(){
//Callback triggered when server is successfully listening. Hurray!
console.log("Server listening on: http://localhost:%s", PORT);
});