在Express Router中动态更改模型/架构

时间:2019-10-16 12:59:14

标签: javascript node.js express

我有一个快递服务器,它接受api调用并将它们指向特定的路由。

app.use('/api/data01', require('./routes/dataRoute01'))
app.use('/api/data02', require('./routes/dataRoute02'))
app.use('/api/data03', require('./routes/dataRoute03'))

每条路线中的方法都相同,除了它们所调用的模型之外。

因此,dataRoute01.js具有如下方法:

//CREATE
router.post('/', (req, res, next) => {
  let { name, details, content, timestamp } = req.body
  Data01.create({ name, details, content, timestamp })
    .then(data => {
      res.json({
        success: true,
        data,
      })
    })
    .catch(err => next(err))
})

dataRoute02.js具有这样的方法:

//CREATE
router.post('/', (req, res, next) => {
  let { name, details, content, timestamp } = req.body
  Data02.create({ name, details, content, timestamp })
    .then(data => {
      res.json({
        success: true,
        data,
      })
    })
    .catch(err => next(err))
})

是否可以将路由合并到一个文件中,而是动态地更改要调用的模型?也许通过在请求中传递一个唯一的标识符?

也许与以下内容类似
(添加到source中,它将成为请求的来源。它可以是一个数字。例如1

将所有api调用指向同一路由。

app.use('/api/data01', require('./routes/dataRoute'))
app.use('/api/data02', require('./routes/dataRoute'))
app.use('/api/data03', require('./routes/dataRoute'))

然后在路线中执行以下操作:

//CREATE
router.post('/', (req, res, next) => {
  const source = req.body.source
  const schema = 'Data0' + source
  let { name, details, content, timestamp } = req.body
  schema.create({ name, details, content, timestamp })
    .then(data => {
      res.json({
        success: true,
        data,
      })
    })
    .catch(err => next(err))
})

编辑:
根据以下答案创建了新的尝试。
api调用中也添加了,因为我认为它将有助于解决问题。

//api.js

  addData(source, body) {
    const url = '/data0' + source
    return service
      .post(url, body) //body here also contains the source
      .then(res => res.data)
      .catch(errHandler)
  },
//dataRoute.js

const express = require('express')
const Data01 = require('../models/Data01')
const Data02 = require('../models/Data02')
const Data03 = require('../models/Data03')

const router = express.Router()

//CREATE
router.post('/', (req, res, next) => {
  let { name, details, content, timestamp } = req.body
  Data0[req.body.source]
    .create({ name, details, content, timestamp })
    .then(data => {
      res.json({
        success: true,
        data,
      })
    })
    .catch(err => next(err))
})

但是.create之前使用的括号符号似乎被忽略了,从而导致如下错误响应:

API response {stack: "ReferenceError: Data0 is not defined↵    at router…r\node_modules\express\lib\router\index.js:635:15", message: "Data0 is not defined"}

2 个答案:

答案 0 :(得分:0)

您可以通过将所有数据保存在单个文件中来实现此目的。

data.js

module.expoprts = {
  Data01: {}, //someObject or you can require another module here
  Data02: {}, //someObject or you can require another module here
  Data03: {}, //someObject or you can require another module here
  ...
}

然后像这样重写代码

const Data = require('./data'); // assuming it is kept in same directory

//CREATE
router.post('/', (req, res, next) => {
  const source = req.body.source
  const schema = 'Data0' + source
  let { name, details, content, timestamp } = req.body
  Date[schema].create({ name, details, content, timestamp })
    .then(data => {
      res.json({
        success: true,
        data,
      })
    })
    .catch(err => next(err))
})

或者如果您已经将所有Data对象都放在一个地方,则可以使用

这里的关键只是用于访问对象属性的基于索引的符号。

阅读this了解更多详细信息

更新

您已经通过数据模式更新了问题

让我在这里解释您的错误

//dataRoute.js

const express = require('express')
const Data01 = require('../models/Data01')
const Data02 = require('../models/Data02')
const Data03 = require('../models/Data03')

const router = express.Router()

//CREATE
router.post('/', (req, res, next) => {
  let { name, details, content, timestamp } = req.body
  Data0[req.body.source]
    .create({ name, details, content, timestamp })
    .then(data => {
      res.json({
        success: true,
        data,
      })
    })
    .catch(err => next(err))
})

这里有三个对象Data01Data02Data03

您正在像这样Data0[...]来访问它,但是没有像Data0这样的变量,这就是为什么要获取"ReferenceError: Data0 is not defined"的原因。

因此,您可以通过创建一个通用的Data对象来解决此问题,该对象将具有所有像这样的数据源

const express = require('express')
const Data01 = require('../models/Data01')
const Data02 = require('../models/Data02')
const Data03 = require('../models/Data03')

const Data = {Data01, Data02, Data03} // created one single container object to hold all data sources.

const router = express.Router()

//CREATE
router.post('/', (req, res, next) => {
  let { name, details, content, timestamp } = req.body
  // Access property based on source from Data container
  const schema = 'Data0' + source
  Data[schema]
    .create({ name, details, content, timestamp })
    .then(data => {
      res.json({
        success: true,
        data,
      })
    })
    .catch(err => next(err))
})


答案 1 :(得分:0)

Schemas.js

const Schema01 = require('./Schema01');
const Schema02 = require('./Schema02');
const Schema03 = require('./Schema03');

modules.exports = {
  Data01, Data02, Data03
}

router.js

const Schemas = require('./Schemas');

router.post('/api/data/:schema', (req, res, next) => {
  let { name, details, content, timestamp } = req.body;
  Schemas[req.params.schema].create({ name, details, content, timestamp })
    .then(data => {
      res.json({
        success: true,
        data,
      })
    })
    .catch(err => next(err))
})