1

我对整个后端业务都很陌生,我想知道是否有人能看出我哪里出错了。我的Nuxt应用程序中有一个快速服务器,它提供 API。当我运行 localhost:3000/api/salesforce/:id 路由时 - 我的 vscode 终端会生成响应 - 但它不会显示在浏览器上。这反过来又使 Nuxt 无法访问它。

在我的nuxt.config.js 中

  serverMiddleware: {
    '/api': '~/api'
  },

/api/index.js

const express = require('express')

// Create express instance
const app = express()

// Require API routes
const users = require('./routes/users')
const test = require('./routes/test')
const salesforce = require('./routes/salesforce')

// Import API Routes
app.use(users)
app.use(test)
app.use(salesforce)

// Export express app
module.exports = app

// Start standalone server if directly running
if (require.main === module) {
  const port = process.env.PORT || 3001
  app.listen(port, () => {
    // eslint-disable-next-line no-console
    console.log(`API server listening on port ${port}`)
  })
}

然后在/api/routes/salesforce.js

const e = require('express')
const { Router } = require('express')

const router = Router()

const jsforce = require('jsforce');

// require the .env file for login
require('dotenv').config();
const { SF_USERNAME, SF_PASSWORD, SF_TOKEN, SF_LOGIN_URL } = process.env;
if (!(SF_USERNAME && SF_PASSWORD && SF_TOKEN && SF_LOGIN_URL)) {
  console.error(
      'Cannot start app: missing mandatory configuration. Check your .env file.'
  );
  process.exit(-1);
}

const conn = new jsforce.Connection({
  loginUrl: SF_LOGIN_URL
});

conn.login(SF_USERNAME, SF_PASSWORD + SF_TOKEN, err => {
  if (err) {
      console.error(err);
      process.exit(-1);
  }
});

// get OPPORTUNITY specific
router.get('/salesforce/:id', function (req, res, next) {

  const id = req.params.id
  console.log(req.params.id)

  const sfData = conn.query(`SELECT Id, Name, StageName FROM Opportunity WHERE Name = '` + id + `'`, (err, res)=>{
    if(err){ 
      console.log(err)
      return "error";
    } else {
      console.log(res.records[0]) 
      let sanitisedData = res.records[0]
      return sanitisedData;
    }
  })
  res.json(sfData.json)

})

如果有人能告诉我哪里出错了,我将不胜感激,我有点卡在这里。

4

0 回答 0