我在nodeJ中开发了一些我想转移到GCP的服务,问题是我使用express.router实现了它,有没有一种方法可以移动/配置GCP端点来使用express.router路由我的呼叫,或者我需要去掉那个路由器,以更直接的方式进行路由?
如果可能的话,您需要向包json添加express依赖项并导出xrouter函数。
我用这个例子来测试快速路由
package.json
{
"name": "sample-http",
"version": "0.0.1",
"dependencies": {
"express": "^4.16.4"
}
}
index.js
const express = require('express');
const app = express();
var router = express.Router()
// a middleware function with no mount path. This code is executed for every request to the router
router.use(function (req, res, next) {
console.log('Time:', Date.now())
next()
})
// a middleware sub-stack shows request info for any type of HTTP request to the /user/:id path
router.use('/user/:id', function (req, res, next) {
console.log('Request URL:', req.originalUrl)
next()
}, function (req, res, next) {
console.log('Request Type:', req.method)
next()
})
// a middleware sub-stack that handles GET requests to the /user/:id path
router.get('/user/:id', function (req, res, next) {
// if the user ID is 0, skip to the next router
if (req.params.id === '0') next('route')
// otherwise pass control to the next middleware function in this stack
else next()
}, function (req, res, next) {
// render a regular page
res.send('regular')
})
// handler for the /user/:id path, which renders a special page
router.get('/user/:id', function (req, res, next) {
console.log(req.params.id)
res.send('special')
})
// mount the router on the app
app.use('/', router)
exports.xrouter = app;
您需要调用的函数是xrouter