简体   繁体   English

我无法让节点 js 中的快速路由器工作

[英]I'm having trouble getting the express router in node js to work

The normal get method works fine on my main server.js file.普通的get方法在我的主server.js文件上运行良好。 I also use server.get('/favicon.ico', (req, res) => res.status(204));我也使用server.get('/favicon.ico', (req, res) => res.status(204)); to tackle the issue of the favicon request.解决网站图标请求的问题。 It works well, again only on my main server.js file.它工作得很好,同样只在我的主server.js文件上。
When I try to separate the code, by creating a user.js file that handles the api calls, I get:当我尝试分离代码时,通过创建一个处理 api 调用的user.js文件,我得到:

::ffff:127.0.0.1 - GET /favicon.ico HTTP/1.1 404 150 - 1.669 ms
::ffff:127.0.0.1 - GET /users HTTP/1.1 404 144 - 0.454 ms

This is my main file - server.js :这是我的主文件 - server.js

    const express = require('express');
    const morgan = require('morgan');
    const server = express();
    const router = require('../routes/user');
    const mysql = require('mysql');
    
    server.use(morgan('short'));
    
    //server.use(express.static('../public'));
    server.use(express.json());
    server.use(express.urlencoded({ extended: false }));
    
    router.use(router);
    
    server.get('/', (req, res) => {
        console.log("Test!");
        res.send("This works!");
    });  
    
    //server.get('/favicon.ico', (req, res) => res.status(204));
    
    server.listen(3003, () => console.log('Program is running! Port: 3003'));

This is my user.js file:这是我的user.js文件:

    const express = require('express');
    const mysql = require('mysql');
    const router = express.Router();
    
    router.get('/users', (req, res) => {
        res.send('Hello');
    });
    
    router.get('/favicon.ico', (req, res) => res.status(204));
    module.exports = router;

I'm not sure if this is related but I also experience the same problem when trying to server.use(express.static('../public')) .我不确定这是否相关,但我在尝试使用server.use(express.static('../public'))时也遇到了同样的问题。 It doesn't work.它不起作用。 I cannot serve my html file.我无法提供我的 html 文件。 I've tried using the csp headers when requesting but that doesn't work.我在请求时尝试使用 csp 标头,但这不起作用。 Setting up a CSP policy in the html header meta tag is not working either.在 html header元标记中设置 CSP 策略也不起作用。

These are the versions of certain modules and technologies if you see a problem in any of their versions:如果您在任何版本中发现问题,这些是某些模块和技术的版本:
Apache 2.4.39 Apache 2.4.39
Node 6.9.0 Windows 7 - Yeah I know but bear with me节点 6.9.0 Windows 7 - 是的,我知道,但请耐心等待

If anyone can help with eigther the router issue or the static file server issue it would much appreciated.如果有人可以帮助解决路由器问题或 static 文件服务器问题,将不胜感激。

Thank you.谢谢你。

A simple way to do this is, in your server.js file add一个简单的方法是,在你的 server.js 文件中添加

server.use("/user", require("./routes/routes"));

note: routes is a folder here and routes.js is a file inside that folder.注意:这里的 routes 是一个文件夹,而 routes.js 是该文件夹内的一个文件。

then in your route.js file add然后在你的 route.js 文件中添加

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

const user = require('../controllers/user.controller');

// register controllers
const userController = new user();
userController.register(router);


module.exports = router;

pass your router in the user register(router) method.在用户注册(路由器)方法中传递您的路由器。 and it will be easily accessible in your user.js file.并且可以在您的 user.js 文件中轻松访问它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM