【问题标题】:routing error Cannot Get /api/name student activity路由错误无法获取 /api/name 学生活动
【发布时间】:2022-02-11 08:04:07
【问题描述】:

我正在尝试使用 Express 重新创建一个笔记应用程序。我的代码遵循讲师的示例,但是当我部署它并尝试添加新注释时,我收到错误 cannot get/api/name...我不确定我做错了什么。

在此处输入代码

const fs = require('fs');
const path = require ('path');
const express = require('express');
const dbJson = require('./db/db.json')
const {v1 : uuidv1} = require('uuid');


const PORT = process.env.PORT || 3001;
const app = express();

app.use(express.urlencoded({extended: true}));

app.use(express.json());
app.use(express.static("public"));

app.get('/notes', (req, res) => {
    res.sendFile(path.join(__dirname, './public/notes.html'));
});

app.get('/api/notes', (req,res) => {
    const dataNotes = fs.readFileSync(path.join(__dirname, './db/db.json'), "utf-8");
    const parseNotes = JSON.parse(dataNotes);
    res.json(parseNotes);
});

【问题讨论】:

    标签: javascript node.js api express


    【解决方案1】:

    最后添加:app.listen(PORT);

    这会奏效。问题是你没有启动服务器

    【讨论】:

    • 谢谢!我让它工作了!
    猜你喜欢
    • 1970-01-01
    • 2016-05-06
    • 2021-03-11
    • 1970-01-01
    • 2012-03-17
    • 1970-01-01
    • 2021-09-25
    • 2022-11-21
    • 2017-08-24
    相关资源
    最近更新 更多