Express.router是否与GCP上的端点兼容?

时间:2020-02-22 23:49:35

标签: google-cloud-platform google-cloud-functions

我在nodeJs中开发了一些要移到GCP的服务,问题是我使用express.router实现了它,是否有一种方法可以移动/配置GCP端点以使用express.router路由我的呼叫,或者摆脱那个路由器并以更直接的方式进行路由?

1 个答案:

答案 0 :(得分:1)

可能的话,您需要向包json添加express依赖关系并导出xrouter函数。

我用这个example测试快递路线

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