user9946765
user9946765

Reputation:

Express routes giving 500 internal server

my server can't find the api's that i created in api directory. it leads to 500 internal server.

I have checked routes.js but i see that everything is right. i have an error.js file for file handling. Here's my code.

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

now showing you my 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'));
};

Upvotes: 0

Views: 1449

Answers (1)

Trinayan
Trinayan

Reputation: 867

I think the problem is in createkeypair file.

Try this code once for your 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'));
};

Do tell me if it works or not.

Upvotes: 2

Related Questions