GET路由仅适用于参数 - node.js + express

时间:2018-02-13 04:30:29

标签: javascript node.js express

我对node + express非常陌生并且无法设置get路由。当我在下面调用的路径中使用参数时,我正在获取数据而没有任何问题。但是,当我尝试在没有参数的情况下返回一个对象时,我从postman&本地主机:3000

我最初认为我的循环导致了这个问题但是当我在console.log中尝试传递res.send的对象时,看起来很好。

下面是我的两个路由(在这个例子中,/ cards / imgs返回一个空白页但/ cards / imgs:id路径正确返回url:

编辑1:在routes.js中定义idIndexes的路由上添加代码

routes.js -

const cards = require('C:/Hearthstone/cards.json');
const data = cards["data"];
var idIndexes = [];
var cardClassIndexes = [];

for (let a in data) {
    idIndexes.push(data[a]["id"]);
};

// index the cards by their "cardClass"
for (let a in data) {
    cardClassIndexes.push(data[a]["cardClass"]);
};

module.exports = (app, db) => {
    // get all card image urls
    app.get('/cards/imgs', (req, res) => {
        const imgURL = 'http://media.services.zam.com/v1/media/byName/hs/cards/enus/'
        var imgArr = {};
        for (let id in idIndexes) {
            var cardID = idIndexes[id];
            var imgURL_Temp = imgURL + cardID + '.png';
            imgArr[cardID] = imgURL_Temp;
        };
        res.set('Content-Type', 'application/json');
        res.send(JSON.stringify(imgArr));
    });

    // get card image url based on id
    app.get('/cards/imgs/:id', (req, res) => {
        const id = req.params.id;
        const imgURL = 'http://media.services.zam.com/v1/media/byName/hs/cards/enus/'
        const imgURL_Temp = imgURL + id + '.png';
        var arr = {};
        arr["url"] = imgURL_Temp
        res.set('Content-Type', 'application/json');
        res.send(arr);
    });

    // get all card information
    app.get('/cards', (req, res) => {
        res.set('Content-Type', 'application/json');
        res.send(JSON.stringify(data));
    });
};

以下是用于API的剩余文件

index.js文件 -

const cardRoutes = require('./routes.js');

module.exports = (app, db) => {
    cardRoutes(app, db);
};

server.js文件 -

const express = require('express');
const MongoClient = require('mongodb').MongoClient;
const bodyParser = require('body-parser');

const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

const port = 3000;

const routes = require('./app/routes');

require('./app/routes')(app, {});

if (app.listen(port)) {
    console.log("Live on port: " + port);
};

cmd

localhost

localhost:3000/cards/

package.json文件 -

{
  "name": "Hearthstone",
  "version": "1.0.0",
  "description": "API for Hearthstone cards",
  "main": "index.js",
  "scripts": {
    "test": "start",
    "dev": "nodemon server.js"
  },
  "author": "michael gross",
  "license": "ISC",
  "dependencies": {
    "body-parser": "^1.18.2",
    "express": "^4.16.2",
    "mongodb": "^3.0.2"
  },
  "devDependencies": {
    "nodemon": "^1.14.12"
  }
 }

2 个答案:

答案 0 :(得分:0)

idIndexes 未定义。

app.get('/cards/imgs', (req, res) => {
        const imgURL = 'http://media.services.zam.com/v1/media/byName/hs/cards/enus/'
        var imgArr = {};
        for (let id in idIndexes) { //here
            var cardID = idIndexes[id];
            var imgURL_Temp = imgURL + cardID + '.png';
            imgArr[cardID] = imgURL_Temp;
        };
        res.set('Content-Type', 'application/json');
        res.send(JSON.stringify(imgArr));
        });

答案 1 :(得分:0)

尝试像这样更改你的routes.js文件......

routes.js

module.exports = (app, db) => {
    // get all card image urls
    app.get('/cards/imgs', (req, res) => {
        const imgURL = 'http://media.services.zam.com/v1/media/byName/hs/cards/enus/'
        var imgArr = {};
        for (let id in idIndexes) {
            var cardID = idIndexes[id];
            var imgURL_Temp = imgURL + cardID + '.png';
            imgArr[cardID] = imgURL_Temp;
        };
        res.status(200).send(imgArr);
    });

    // get card image url based on id
    app.get('/cards/imgs/:id', (req, res) => {
        const id = req.params.id;
        const imgURL = 'http://media.services.zam.com/v1/media/byName/hs/cards/enus/'
        const imgURL_Temp = imgURL + id + '.png';
        var arr = {};
        arr["url"] = imgURL_Temp
        res.status(200).send(arr);
    });
};