gpt4 book ai didi

javascript - Express js 路由器不工作

转载 作者:行者123 更新时间:2023-12-03 03:27:12 25 4
gpt4 key购买 nike

我在 Express js 路由方面遇到了困难。我有一个用户路由文件,我在 app.js (主文件)中需要该文件。但它总是给出 Cannot GET/auth/register.

我的用户路线如下:

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

router.get('register', (req, res, next) => {
console.log('Hello');
res.send('Register');
});
module.exports = router;

我的应用程序文件如下:

const express    = require('express');
const path = require('path');
const bodyParser = require('body-parser');
const cors = require('cors');
const passport = require('passport');
const mongoose = require('mongoose');

const app = express();

const config = require('./config/app');
const userRoutes = require('./routes/users');

//connecting with mongo DB
mongoose.connect(config.database);

// On successful connection
mongoose.connection.on('connected', () => {
console.log('Connected');
});

// On Error while connecting
mongoose.connection.on('error', (err) => {
console.log(err);
});

// Port of express server
const PORT = config.port;

// Adding cors middleware
app.use(cors());

// Adding Body Parser Middle ware
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

// Adding user Routes

app.use('/auth', userRoutes);

// Start the express server
app.listen(PORT, function(err) {
if (err) throw err;
console.log("Server is Running on port " + PORT);
});

最佳答案

您忘记了用户路径中的符号“/”。试试这个:

router.get('/register', (req, res) => {
console.log('Hello');
res.send('Register');
});

关于javascript - Express js 路由器不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46264803/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com