Nodejs http://localhost:8080无法工作。

3

我是nodejs的初学者,从w3school学习。之前我使用PHP工作。问题是,当我运行一个简单的nodejs程序时,在命令行和Web浏览器中没有任何反应。

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);  

我已经在使用xampp服务器来运行apache。
如何在系统中同时运行两者?

你尝试执行的命令是什么?用于更改Xampp服务器端口 https://dev59.com/Smgu5IYBdhLWcg3we3Ct - Sathishkumar Rakkiyasamy
如果端口(8080)已被分配,则无法使用相同的端口,请尝试更改您的端口并检查。 - Priyank
1个回答

2
如果您正在使用Node.js的express框架,则尝试使用以下代码在您的Node.js应用程序的主js文件app.js中运行Node.js程序。
const express = require('express')
const app = express()

app.get('/', function (req, res) {
  res.send('Hello World!')
})

app.listen(8080, function () {
  console.log('Example app listening on port 8080!')
})

然后从命令提示符中运行以下命令:
-> node (path to app.js)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接