javascript - 什么是相当于 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");
});
非常感谢您的帮助!!!!
解决方案
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");
});
推荐阅读
- pandas - 那么我可以在 pd.date_range 中使用输入数据吗?
- javascript - 如何在订单中实现 if 语句
- reactjs - 如何使汉堡图标菜单功能在 React.js 中单击即可使用?
- ios - 收到推送通知时,iOS 应用程序中未调用警报消息
- angular - 路由更改后单例服务重置
- elasticsearch - 如何使用弹性搜索突出显示单词中的字符
- react-native - 适用于 Android Wear OS 或 Samsung Tizen OS 的 React Native
- react-native - React Native:在 iOS 上的 TextInput 中更改文本和表情符号字体大小
- c# - 如何从任务 httpResponse 中获取信息?
- php - PHP变量超出HTML链接href字符串?