快速路由提供500个内部服务器

时间:2019-02-18 10:46:21

标签: javascript api express http-status-code-500

我的服务器找不到我在api目录中创建的api。它导致500个内部服务器。

我检查了route.js,但我发现一切都正确。我有一个error.js文件用于文件处理。这是我的代码。

'use strict';

let router = require('express').Router();

// Middleware
let middleware = require('./controllers/middleware');
router.use(middleware.doSomethingInteresting);

// Tasks
let tasks = require('./controllers/tasks');
let createkeypairs = require('./controllers/createkeypairs');
let importaddress = require('./controllers/importaddress');
let getwalletinfo = require('./controllers/getwalletinfo');

router.get('/tasks', tasks.findAll2);
router.get('/createkeypairs', createkeypairs.findAll);
router.get('/importaddress', importaddress.findAll);
router.get('/getwalletinfo', getwalletinfo.findAll);
router.post('/buggyroute', tasks.buggyRoute);



// Error Handling
let errors = require('./controllers/errors');
router.use(errors.errorHandler);

// Request was not picked up by a route, send 404
router.use(errors.nullRoute);

// Export the router
module.exports = router;

现在向您展示我的createkeypairs.js

'use strict';

let errors = require('./errors.js');
var request = require("request");
var options = { method: 'POST',
    url: '127.0.0.1:18332',
    headers: 
    {  'Authorization': 'Basic bXVsdGljaGFpbnJwYzpHTmJ5enJhMnlHRjN4Ymp1cnluRTFucTlnV1ExRXV3OTFpYVBqSkt5TkJxdA==',
    'cache-control': 'no-cache',
    'Cache-Control': 'no-cache',
    'Content-Type': 'application/json' },
    body: { method: 'createkeypairs', params: [], chain_name: 'tokenchain' },
    json: true };

exports.findAll = (req, res, next) => {
// Simulate task list, normally this would be retrieved from a database
let createkeypairs ;
request(options, function (error, response, body) {
    if (error) throw new Error(error);

    console.log("working here ");
    // res.json(body);

});

};

exports.buggyRoute = (req, res, next) => {
    // Simulate a custom error
    next(errors.newHttpError(400, 'bad request'));
};

1 个答案:

答案 0 :(得分:1)

我认为问题出在createkeypair文件中。

为您的createkeypairs.js尝试一次此代码:

'use strict';

let errors = require('./errors.js');
var request = require("request");
let config = require('config');

var auth = 'Basic ' + Buffer.from(config.user + ':' + config.pass).toString('base64');
var url = config.url;
var chain = config.chain;

var options = { method: 'POST',
    url: url,
    headers: 
     { 'cache-control': 'no-cache',
            Authorization : auth,
         'Content-Type': 'application/json' },
    body: { method: 'importaddress', params: ["address"], chain_name: chain },
    json: true };

exports.findAll = (req, res, next) => {
    // Simulate task list, normally this would be retrieved from a database
    let createkeypairs ;

request(options, function (error, response, body) {
    if (error) throw new Error(error);

    console.log(body);
    res.json(body);
});
};

exports.buggyRoute = (req, res, next) => {
    // Simulate a custom error
    next(errors.newHttpError(400, 'bad request'));
};

请告诉我它是否有效。