javascript Express应用程序(使用TypeScript)未响应路径

bxpogfeg  于 2023-02-18  发布在  Java
关注(0)|答案(1)|浏览(98)

我有以下问题:
我正在做一个API,但是当我向端点发出请求时,我的应用程序甚至没有进入操作?
这是我的服务器设置

import express, { Express } from 'express';
import cookieParser from 'cookie-parser';
import cors from 'cors';
import characterRouter from '../modules/character/router';
import userRouter from '../modules/user/router';
import itemsRouter from '../modules/item/router';

require('dotenv').config();

const app: Express = express();

app.use(cors);

//Setup cookieParser and public folder
app.use(cookieParser());

//Setup bodyParser
app.use(express.json());
app.use(express.urlencoded({ extended: false }));

app.get('/items', (req, res) => {
    console.log('OK');
    res.json({ ok: true });
});

app.listen(3000, () => {
    console.log(`Server listnening on port ${process.env.SERVER_PORT}`);
});

这是我第一次使用导入语法,可能是因为它吗?
当我请求GET localhost:3000/items时,它甚至不会转到console.log
子配置:

{
    "compilerOptions": {
        "target": "es2016",
        "module": "commonjs",
        "outDir": "./dist",
        "esModuleInterop": true, 
        "forceConsistentCasingInFileNames": true,
        "strict": true, 
        "skipLibCheck": true,
    }
}
a8jjtwal

a8jjtwal1#

import express, { Express } from 'express';
import cookieParser from 'cookie-parser';
import cors from 'cors';
import characterRouter from '../modules/character/router';
import userRouter from '../modules/user/router';
import itemsRouter from '../modules/item/router';

require('dotenv').config();

const app: Express = express();

app.use(cors);

//Setup cookieParser and public folder
app.use(cookieParser());

//Setup bodyParser
app.use(express.json());
app.use(express.urlencoded({ extended: false }));

// You missing
app.use('/', indexRouter);
app.use('/users', usersRouter);

app.get('/items', (req, res) => {
    console.log('OK');
    res.json({ ok: true });
});

app.listen(3000, () => {
    console.log(`Server listnening on port ${process.env.SERVER_PORT}`);
});

相关问题