【问题标题】:Building an API in Express.js, Getting "Cannot GET /api"在 Express.js 中构建 API,得到“Cannot GET /api”
【发布时间】:2019-07-22 05:17:46
【问题描述】:

输入查询时无法获取 /api,我转到 localhost:3000 并得到我期望返回的内容,即“随机用户 API”,但是当我输入查询时 http://localhost:3000/api?results=100。我得到了 Cant GET/api。不知道从哪里来的

index.js

let express = require('express'),
route = require('./routes/routes.js');

let app = express();

app.use(function(req, res, next) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
    next();
});

app.use(express.static(__dirname + '/public'));

app.get('/', route.index);
app.get('/app', route.api);

app.listen(3000);

routes.js

var myNames = require("../names.json");

var MAX_RESULTS = 1000;

exports.index = function(req, res) {
  res.send("Random User API");
};

exports.api = function(req, res) {
  this.results = req.query.results;
  this.seed = req.query.seed;
  res.send(generateNames(this.results, this.seed));
};

function generateNames(results, seed) {
  this.results = results;
  if (this.results > MAX_RESULTS) {
    this.results = MAX_RESULTS;
  }
  this.seed = seed;

  var rand = require("random-seed").create(this.seed);
  var gender;
  var picRand;
  var picNum;
  var genRand;
  var firstname, lastname, address, city, state, postcode;
  var username;
  var phone;

  var names = {
    results: []
  };
  for (let i = 0; i < this.results; i++) {
    genRand - rand(2);
    picRand = rand(150) + 1;

    if (picRand < 10) {
      picNum = "00" + picRand;
    } else if (picRand < 100) {
      picNum = "0" + picRand;
    } else {
      picNum = "" + picRand;
    }

    if (genRand == 0) {
      gender = "male";
      picLarge = "http://localhost:3000/images/m" + picNum + ".jpg";
      firstname = myNames.male_names[rand(myNames.male_names.length)];
    } else {
      gender = "female";
      picLarge = "http://localhost:3000/images/f" + picNum + ".jpg";
      firstname = myNames.female_names[rand(myNames.female_names.length)];
    }

    lastname = myNames.last_names[rand(myNames.last_name.length)];

    address =
      myNames.last_names[rand(myNames.last_names.length)] +
      " " +
      myNames.street_types[rand(myNames.street_types.length)];

    postcode = rand(90000) + 10000;
    phone = rand(999) + 120 + "-" + rand(999) + 100 + "-" + rand(9999) + 1000;

    var person = {
      gender: gender,
      name: {
        first: firstname,
        last: lastname
      },
      location: {
        street: address,
        city: city,
        state: state,
        postcode: postcode
      },
      email: email,
      login: {
        username: username,
        password: password
      },
      dob: {
        date: date,
        age: age
      },
      phone: phone,
      cell: cell,
      picture: {
        large: picLarge,
        medium: picMedium,
        thumbnail: thumbnail
      }
    };
    names.results.push(person);
  }
  return names;
}

我希望我的数据以 JSON 格式显示

【问题讨论】:

    标签: javascript api express


    【解决方案1】:

    您正在使用 /app 而不是 /api

    app.get('/app', route.api);
    

    【讨论】:

    • 谢谢你,不敢相信我错过了哈哈
    猜你喜欢
    • 2022-07-31
    • 2014-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-16
    • 1970-01-01
    • 2021-12-25
    • 2020-01-05
    相关资源
    最近更新 更多