Javascript 服务器未传递响应
我是node.js的新手,我想知道为什么它没有按计划工作。我曾经Javascript 服务器未传递响应,javascript,html,node.js,express,Javascript,Html,Node.js,Express,我是node.js的新手,我想知道为什么它没有按计划工作。我曾经 res.end("Hello world") 过去在页面上显示“Hello World” 现在,使用下面的代码,它似乎正在监听同一个地址,但我在那里导航,它不会转到页面!这就像是没有做出回应。我很困惑 web.js index.html(同一目录) 您实际上没有使用express。您正在将路由附加到它,但您正在创建一个单独的服务器对象,并让进行侦听。你应该有更像这样的东西: const http = require('http'
res.end("Hello world")
过去在页面上显示“Hello World”
现在,使用下面的代码,它似乎正在监听同一个地址,但我在那里导航,它不会转到页面!这就像是没有做出回应。我很困惑
web.js
index.html(同一目录)
您实际上没有使用express。您正在将路由附加到它,但您正在创建一个单独的服务器对象,并让进行侦听。你应该有更像这样的东西:
const http = require('http');
var express = require('express');
var app = express();
const hostname = '127.0.0.1';
const port = 3000;
app.get('/', function(request, response){
response.sendFile('index.html');
});
// Have the actual express app listen, not server
app.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
Express的目的是包装node.js中创建服务器所涉及的锅炉板。看看Express。该示例甚至不需要模块:
const express = require('express')
const app = express()
app.get('/', function (req, res) {
res.send('Hello World!')
})
app.listen(3000, function () {
console.log('Example app listening on port 3000!')
})
您实际上没有使用express。您正在将路由附加到它,但您正在创建一个单独的服务器对象,并让进行侦听。你应该有更像这样的东西:
const http = require('http');
var express = require('express');
var app = express();
const hostname = '127.0.0.1';
const port = 3000;
app.get('/', function(request, response){
response.sendFile('index.html');
});
// Have the actual express app listen, not server
app.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
Express的目的是包装node.js中创建服务器所涉及的锅炉板。看看Express。该示例甚至不需要模块:
const express = require('express')
const app = express()
app.get('/', function (req, res) {
res.send('Hello World!')
})
app.listen(3000, function () {
console.log('Example app listening on port 3000!')
})
有错误信息吗?没有!抱歉,我将在浏览器的调试器中包含输出和?请尝试res.sendFile(path.join(uu dirname+'/index.html');更像是response.sendFile…有任何错误消息吗?没有!抱歉,我将在浏览器的调试器中包含输出和?请尝试res.sendFile(path.join(uu dirname+'/index.html');更像是,response.sendFile…非常感谢!我会进一步研究。似乎我对express模块的实际功能感到困惑非常感谢!我会进一步研究。似乎我对express模块的实际功能感到困惑
const express = require('express')
const app = express()
app.get('/', function (req, res) {
res.send('Hello World!')
})
app.listen(3000, function () {
console.log('Example app listening on port 3000!')
})