Routes for Express are throwing a 500 internal server error

My server is unable to locate the APIs that I have created in the API directory, which is resulting in a 500 internal server error.

I have thoroughly checked routes.js and everything appears to be correct. Additionally, I have an error.js file for handling errors. Below is 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);

// If request was not picked up by any route, send 404
router.use(errors.nullRoute);

// Export the router
module.exports = router;

Now I will show you my createkeypairs.js file:

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

Answer №1

It seems like the issue lies within the createkeypair file.

Feel free to try out this updated code snippet 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'));
};

Please let me know if this resolves the issue.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

{ "error": "The 'title' property of 'req.body' cannot be destructured because it is not defined." }

Here are the snippets of code for a Node.js application: // src/models/workoutsModel.js const mongoose = require("mongoose"); const Schema = mongoose.Schema; const workoutSchema = new Schema({ title: { type: String, required: true, ...

Performing multiple ajax calls simultaneously in JavaScript using the React framework

Within my React application, I am faced with the challenge of handling an array of parameters (such as IDs) that need to be passed as parameters in a queue of ajax calls. The issue arises when this array exceeds 1000 items, causing the browser page to beco ...

I am experiencing an issue with my React app deployed on Heroku where it successfully loads the home page but fails

My react application performs perfectly when run locally and is successfully deployed on heroku. However, upon clicking any link from the home page to another page, a blank page with 'not found' message appears. No other error messages are displa ...

The periodLookup array does not have a defined value for periodStr. Why is this error being caught?

Here is a method that I am working with: set_period_help_text: function(periodInput){ var metric = MonitorMetric.getSelectedMetric(); var periodStr = $('select[name=metric_period]').val(); var datapoints = Number(periodIn ...

JavaScript and jQuery validation issues persisting

Here is the HTML code I am using: <script src="js/validate.js"></script> <label>FIRST NAME:</label> <td> <input type="text" class="firstname" id="firstname" onKeyUp="firstname()" /> </td> <td> <label id=" ...

Only show the directive methods when using "ng-if"

I am facing an issue with the Opentoke Library directive, particularly when I use the ng-if. The reason for implementing the ng-if is that WebRTC is not supported on IOS devices, so it displays an alert after the DOM has loaded. <div class="opentok" ng ...

Is it possible to have a hidden div box within a WordPress post that is only visible to the author of the

How can I create a div box with "id=secret" inside a post that is only visible to the author of the post? I initially made it for the admin, but now I want the id to be visible exclusively to the post's author. For instance: If the author is curren ...

How can I configure Express to act as a pass-through proxy server?

How can I set up an express server to act as a proxy? Requirements: Support for both http and https Ability to function behind a corporate proxy Option to provide custom content for specific URLs I have experimented with various modules such as http-pr ...

AngularJS to validate image dimensions (width and height) on upload

Hello, I am new to working with Angular JS. I have been experimenting with a login form that includes a file upload field for the user picture. The input field code looks like this: <input type="file" file-model="userPic"/> When I submit the form i ...

swap out the CSS class for my own class dynamically

When I display HTML code like this <div class="btn btn-pagination"> <i class="fa fa-angle-right"></i> </div> and want to replace fa fa-angle-right with myClass when the page loads. I attempted: $(document).ready(function () { ...

Sorting in Vuejs fails to function properly when a filter is applied

Having recently delved into Laravel and Vue, I am eager to develop a site for our Intranet. Pulling data from the Laravel database has been successful, displaying the data works well, and the search functionality is also up and running smoothly. However, I ...

The anonymous function in the Google strategy is not being executed

I am currently working on implementing Passport to allow users to log in to my website using their Google accounts. I am utilizing yarn along with the following relevant packages: [email protected], and passport-google-oauth20@^1.0.0. The issue I am f ...

Changing the event handler of a jQueryUI accordion dynamically from "click" to "mouseover" as needed

I am facing a scenario where I need to drag an item from a list and drop it into a target within an accordion. The challenge is that the target may be in a panel that is not currently open. To address this issue, I am looking to dynamically switch the acc ...

Express.JS - Creating Customized URLs for Dynamic Routes

I am in the process of developing a social network platform where users can connect with others by logging in to their accounts. The account access will be available through the following pathways: https://domain.com/en/community/id/:userid AND https://do ...

Ways to ensure your Javascript code only runs based on the specific browser

I need a Javascript code to run depending on the browser version. Specifically, if the browser is not IE or is IE 9+, one piece of Javascript should be executed. If the browser is IE8 or lower, another piece of Javascript should be executed. My attempt to ...

The process of deleting lines from an image using Javascript

If I have an image of a data-table and I want to eliminate all the grid lines (defined as continuous vertical or horizontal pixels), is there a way to achieve this using Javascript's image data manipulation? I envision looping through a 2D array conta ...

Using HTML and JavaScript to add variables into the URL within the window.location

I have been struggling to incorporate longitude and latitude into the URL. Despite researching various topics online, I have not found a solution that works for me. Below is the HTML code that showcases the issue. When you click the "Show Position" button ...

Accessing getUserMedia within Internet Explorer 11 or utilizing MediaStream on IE 11

I am attempting to utilize the getUserMedia function in IE 11 with the help of the temasys plugin. However, IE does not support the MediaStream instance returned by getUserMedia. Below is a snippet of my code: import React from 'react' import { ...

The console is reporting that the term "next" has not been

I am currently working on a project and I am trying to validate my jwt token using a middleware. However, I encountered an error message in the console stating "next is not defined". Here is the code snippet: In login.js const verifyToken = require(&apos ...

Exploring the functionality of Next.js with Links and routes

Currently, I am facing an issue with the popover menu in my header that displays products. The problem arises when I click on a product in the list; it navigates correctly to the path "products/some-product" regardless of the selected item. However, if I a ...