【问题标题】:Nodejs http://localhost:8080 not workingNodejs http://localhost:8080 不工作
【发布时间】:2018-07-24 13:57:54
【问题描述】:

我是 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 服务器端口 stackoverflow.com/questions/11294812/…
  • 如果端口(8080)已经分配,​​那么你不能使用同一个端口尝试改变你的端口并检查。

标签: php node.js localhost


【解决方案1】:

如果您使用的是 Node js 的 express 框架,请尝试使用以下代码在 app.js 中运行 NodeJs 程序(您的 NodeJs 应用程序的主 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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-20
    • 2016-06-28
    • 1970-01-01
    • 2016-10-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多