我正在尝试使用Express制作一个简单的hello-world类型的服务器。这是我的代码:
const express = require("express");
const app = express();
app.get("/",function(request, response){//what to do when someone make get request to the homepage or route
console.log(request);
response.send("hello");
});
app.listen(3000, function(){
console.log("server is listening at port 3000");
});
当我运行程序时,我在命令提示符下看到:
server is listening at port 3000
但当我通过浏览器(即https://localhost:3000
(访问它时,我会收到一个错误:
此站点无法提供安全连接本地主机发送了一个无效的回答请尝试运行Windows网络诊断。ERR_SSL_procol_ERROR
我希望浏览器按照我的方法看到hello
,如上所述,response.send("hello")
您需要通过HTTP
协议来访问它。尝试连接到http://localhost:3000
而不是https://localhost:3000
。
app.listen
的基本功能是创建一个http服务器并侦听它,因此必须使用http
协议而不是https
来访问它。
这是应用程序侦听的源代码,取自express:app.listen