javascript - 提供 500 个内部服务器的 Express 路线

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

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

我检查了routes.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'));
};

最佳答案

我认为问题出在 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'));
};

请告诉我它是否有效。

关于javascript - 提供 500 个内部服务器的 Express 路线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54745493/

相关文章:

javascript - 从 .json 返回获取 javascript var

web-services - Salesforce 不同的 WSDL 文件以及何时使用

api - 如何检索用户的所有推文,而不仅仅是前 3,200 条推文,因为 Twitter 将其时间线和 API 限制为

ruby - 为 Savon 2 (SOAP) 传递数组元素

node.js - 使用模式方法在 Mongoose For 循环中保存项目

javascript - 卡片翻转动画 Internet Explorer 11

javascript - 如果从另一个函数调用,重新加载 div 不会更新时间

node.js - 如果我在 Node.js 后端渲染 React 组件,如何向它们添加加载微调器?

javascript - 在javascript中显示hh :mm:ss. msmsms

node.js - 使用 SocketIO 和 Express 进行 Node mocha API 测试