gpt4 book ai didi

php - Nodejs http ://localhost:8080 not working

转载 作者:太空宇宙 更新时间:2023-11-03 22:08:44 26 4
gpt4 key购买 nike

我是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。

如何在系统中运行两者。

最佳答案

如果您使用的是 Node js 的 Express 框架,请尝试使用以下代码在 app.js(您的 NodeJs 应用程序的主 js 文件)中运行 NodeJs 程序

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)

关于php - Nodejs http ://localhost:8080 not working,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48779660/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com