2017-07-28 34 views
0

首先抱歉,這是一個初學者的問題,但我似乎無法自行離開。 我正在開發一個應用程序,它將根據Json中的一個特定參數來發送消息。我在app.config.jsonNode.js路由出錯

<code> 
{ 
    "server": { 
     "port": "8080" 
    }, 
    "auth":{ 
     "users": { 
      "xxx": "xxxxx" 
     }, 
     "unauthorizedResponse": "Unauthorized!" 
    }, 
    "services": { 
    "service one": { 
     "address": "http://localhost", 
     "port": "8000" 
    } 

} 


} 
</code> 

以下和路由器是:

<code> 
const request = require('request'); 
const express = require("express"); 
const router = express.Router(); 
const fs = require("fs"); 
const configs = JSON.parse(fs.readFileSync(__dirname + '/../app.config.json', 'utf8')); 

const services = configs.services; 



router.post('/', (req, res) => { 
    let serviceName = req.get('service-name'); 

    if (services[serviceName]) { 

     let requestOptions = { 
      uri: services[serviceName].address + ":" + services[serviceName].port, 
      json: req.body 
     } 
     request.post(requestOptions, (error, response, body) => { 

      res.send(body); 
     }); 
    } else { 
     res.send("There is no service named " + req.get("service-name")) 
    } 

}); 

module.exports = router; 

</code> 

我的問題是我想更多的服務app.config.json該結束了給我加在JSON對象中出現以下錯誤:

undefined:20 
    "service2": { 
    ^

SyntaxError: Unexpected string in JSON at position 313 
</code> 

Any ideas?

Thank you in advance.

+0

工作!謝謝你很多! – Spanglish

回答

0

Maybe you are missing a ,。讓我們來試試這個:

"service one": { 
    "address": "http://localhost", 
    "port": "8000" 
}, 
"service two": { 
     "address": "http://localhost/route2", 
     "port": "8000" 
    }