本地主机未发送任何数据-ERR_EMPTY_RESPONSE [英] localhost didn't send any data - ERR_EMPTY_RESPONSE

查看:3183
本文介绍了本地主机未发送任何数据-ERR_EMPTY_RESPONSE的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用VScode编写NodeJS

Using VScode to write NodeJS

但是要检查,本地主机没有正确响应,它说:该页面未发送任何数据. 我尝试到处寻找并更改了所有可能的设置.

But to check, localhost isn't responding properly, it says "the page didn't send any data. I tried looking everywhere and changed all the possible settings I can.

还原的Chrome设置 清除缓存,清除历史记录,更改端口号,更改局域网设置.

restored chrome settings cleared cache, cleared history, changed port numbers, changed LAN settings.

 ''''

const Joi = require('joi');
const express = require('express');
const app = express();

app.use(express.json); //to take inputs ffrom the browser
const courses = [
{id: 1, name: "course 1"},
{id: 2, name: "course 2"},
{id: 3, name: "course 3"},
 ];

app.get('/', (req, res) =>{
res.send("Hello world!!");
res.end();
});

app.get('/api/courses/', (req, res) =>{
res.send(courses);
res.end();
});

const port = process.env.port || 8080;
app.listen(port, ()=> console.log(`Listining on port ${port}..`));

 ''''

想看所有印刷在网页上的课程.

Want to see all the courses printed on the webpage.

推荐答案

您的courses是一个对象.您需要通过电线将其作为字符串发送.将其作为json发送,浏览器将能够对其进行解析.

Your courses is an object. You need to send it as string over wire. Send it as json and the browser will be able to parse it.

app.get('/api/courses/', (req, res) => {
  // change here, your object is stringified to json by express
  res.json(courses);
});

要从浏览器获取输入,您必须使用body-parser之类的软件包,而不要使用app.use(express.json)

To take input from browser, you'd have to use packages like body-parser and not app.use(express.json)

var express = require('express')
var bodyParser = require('body-parser')

var app = express()

// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }))

// parse application/json
app.use(bodyParser.json())

这篇关于本地主机未发送任何数据-ERR_EMPTY_RESPONSE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆