[英]How to import routes in polka js similar to express.Route()
I am trying to import route logic from another file.我正在尝试从另一个文件导入路由逻辑。 In express js this is achievable via express.Route(), when i tried polka.Route() an error pops up saying Route doesn't exist in polka.
在 express js 中,这可以通过 express.Route() 实现,当我尝试 polka.Route() 时,会弹出一个错误,说 Route 在 polka 中不存在。
Express Implementation快速实施
server.js服务器.js
const express = require('express');
const users = require('./routes/api/users');
const app = express();
app.use('/users', users);
user.js用户.js
const express = require('express');
const router = express.Router();
router.get('/test', (req, res) => res.json({ msg: 'works' }));
module.exports = router;
When /users/test
is hit the output is {msg:'works'}.当
/users/test
被点击时,输出是 {msg:'works'}。 This works for the express implementation.这适用于快速实现。 For the polka implementation i changed the word
express
to polka
installing it.对于 polka 实现,我将
express
一词更改为polka
安装。 The issue arises on the line polka.Router() of user.js.问题出现在 user.js 的 polka.Router() 行上。 How do i enable this functionality of importing route logic from another file in polka.
我如何启用从 polka 中的另一个文件导入路由逻辑的功能。
The polka
micro web server does not implement a difference between routers and the app. polka
micro web 服务器没有实现路由器和应用程序之间的区别。 In your users.js
file simply setup your routes as you would in your server.js
file and then module.export
.在您的
users.js
文件中,只需像在server.js
文件中一样设置您的路由,然后设置module.export
。 See Below:见下文:
Polka Implementation波尔卡实现
server.js服务器.js
const polka = require('polka');
const users = require('./routes/api/users');
const app = polka();
app.use('/users', users);
user.js用户.js
const polka = require('polka');
const router = polka();
router.get('/test', (req, res) => res.end(JSON.stringify({ msg: 'works' })));
module.exports = router;
Hope this help!希望这有帮助!
Also, here is a good link to see other differences between Express.js
and Polka.js
: https://github.com/lukeed/polka#comparisons此外,这里有一个很好的链接,可以查看
Express.js
和Polka.js
之间的其他差异: https : //github.com/lukeed/polka#comparisons
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.