简体   繁体   中英

Node.js Error in routing

First of all sorry it is a beginners question yet I cannot seem to get out by myself. I am working on an app that will dispatch messages according to one specific parameter in Json. I have the following in app.config.json

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

}


}
</code>

And the router is:

<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>

My problem is I am trying to add more services to app.config.json that ends up giving me the following error:

undefined:20
    "service2": {
    ^

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

Any ideas?

Thank you in advance.

Maybe you are missing a , in the JSON object. Let's try this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM