首页 > 解决方案 > 什么是相当于 require('./app/routes/posts.routes.js')(app) 的导入语句?

问题描述

我尝试了不同的方法来搜索这个,但我什么也没想到。在设置快速应用程序并导入路线文件时,您经常会看到此语句的文档:

require('./app/routes/posts.routes.js')(app)

从nodejs.org的文档中,这意味着需要文件 './app/routes/posts.routes.js' 并运行 app 对象中的所有内容。

我没有遇到的是如何使用“import”来编写这个语句。

我的例子:

路由文件内容

module.exports = (app) => {

    import posts from '../controllers/post.controller.js'

    app.post('/posts', posts.create);

    app.get('/posts', posts.findAll);

    app.get('/posts/:postId', posts.findOne);

    app.put('/posts/:postId', posts.update);

    app.delete('/posts/:postId', notes.delete);
}

进口声明

//this does not work throws Error: Cannot find package 'app' imported from /Users/matt1/socialbulk/server/server.js

import posts from 'app/routes/post.routes.js' //what do you do with (app)?

app.use('/posts', posts) 

app.listen(3000, () => {
    console.log("Server is listening on port 3000");
});

非常感谢您的帮助!!!!

标签: javascriptnode.jsexpress

解决方案


module.exports = foo相当于export default foo

foo是一个功能

import posts from 'app/routes/post.routes.js' //what do you do with (app)?

app.use('/posts', posts(app)) 

app.listen(3000, () => {
    console.log("Server is listening on port 3000");
});

推荐阅读