我正在try 用NodeJS和Express创建一条简单的Hello World路由.当我运行node index.js时,我在控制台中得到listening on port 3000,但当我转到http://localhost:3000/helloworld时,什么都不起作用.它一直在try 连接.

我可能在路由的某个地方遗漏了什么.

index.js

const express = require("express");
const app = express();
const helloworld_route = require("./routes/helloworld");

const dotenv = require("dotenv");
dotenv.config({ path: "../.env.sample" });
const port = process.env.PORT;

app.use(express.json);

app.listen(port, () => {
  console.log(`Listening on ${process.env.PORT}`);
});


app.use("/", helloworld_route);

controllers/hello.js

const helloTest = (req, res) => {
  res.send("Hello World!");
  console.log("hello");
};


module.exports = {
  helloTest,
};

Routes.helloworld.js

const express = require("express");
const router = express.Router();

const HelloWorld = require("../controllers/hello");


router.get("/helloworld", HelloWorld.helloTest);

module.exports = router;

推荐答案

Express.js express.json()函数

一百零二

Syntax:个 Espress.json([选项])

const express = require("express");
const app = express();
const helloworld_route = require("./routes/helloworld");

const dotenv = require("dotenv");
dotenv.config({ path: "../.env.sample" });
const port = process.env.PORT;

App.use(exts.json());

app.use("/", helloworld_route);


app.listen(port, () => {
console.log(`Listening on ${port}`);
});

你用你的代码**

App.use(exts.json());

**

Javascript相关问答推荐

Codemirsor 6语言包的使用部分是什么?

Express.js:以块形式发送响应

如何在不指定宽度和高度的情况下显示来自网址的下一张图像

Angular 拦截器错误处理删除方法问题

为什么我达到了时间限制!?LeetCode链接列表循环(已解决,但需要解释!)

React Native平面列表自动滚动

为什么getRecord()会因为与_logger相关的错误而失败?(使用Hedera SDK)

Angular:动画不启动

我的服务工作器没有连接到我的Chrome扩展中的内容脚本.我该怎么解决这个问题?

屏幕右侧屏障上的产卵点""

Angular 中的类型错误上不存在获取属性

使用POST请求时,Req.Body为空

Eval vs函数()返回语义

Rxjs流中生成IMMER不能在对象上操作

MongoDB受困于太多的数据

如果没有页面重新加载Angular ,innerHTML属性绑定不会更新

打字脚本中方括号符号属性访问和拾取实用程序的区别

更改agGRID/Reaction中的单元格格式

在JavaScript中,有没有一种方法可以迭代字符串的词法标记?

ReferenceError:无法在初始化之前访问setData