Having trouble grasping the purpose of app.use('/') in the Express framework

Below is the code snippet I am currently working with:

// Initializing express, body-parser, mongodb, http, and cors
var app = express();
app.use(cors());
app.use(express.urlencoded());

// Parse incoming JSON data from API clients
app.use(express.json());

var CONNECTION_URL = "mongodb://localhost:27017";
var DATABASE_NAME = "real_india_db0";
var database = "real_india_db0";
var collection = "db0";

app.listen(3000, () => {
    MongoClient.connect(CONNECTION_URL, { useNewUrlParser: true }, (error, client) => {
        if(error) {
            throw error;
        }
        database = client.db(DATABASE_NAME);
        collection = database.collection("db0");
        console.log("Connected to `" + DATABASE_NAME + "`!");
    });
});

app.post("/enterTasks", (request, response) => {
    var requestBody = [
        {'taskId': 1, 'projectId': 1, 'content' : {'name': 'Task 1', 'predecessors': [], 
        'successor': [{2}] }]
    ];
    console.log("abc ....", requestBody);
});

To run the code, execute:

node app.js

I am making a request to this URL:

http://127.0.0.1:8081/enterTasks

However, the expected message in the developer console doesn't appear:

console.log("abc ....", requestBody)

Can anyone help me understand why this error is occurring?

Answer №1

app.post("/enterTasks", (request, response) => {

 var requestBody = [

    { 'taskId': 1,   'projectId' : 1,  'content' : {'name': 'Task 1' , 'predecessors':[], 
  'successor':  [{2}]  } ]
  console.log("abc ....", requestBody);

 });

In the context of a post request, if you were to change this for a browser request:

app.post()

You would need to replace it with:

app.get()

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

Failure to display React component on screen

I have developed a React microfrontend application consisting of two sub-apps rendered through the container/ project. Both sub-apps render perfectly in isolation on localhost:8083. However, when attempting to view them via localhost:8080/dashboard, I am p ...

What advantages could learning ReactJS first give me before diving into NextJS?

Just mastered TS and now faced with the decision of choosing a framework. I'm curious why it's recommended to learn ReactJS before NextJS. I've read countless articles advising this, but no one seems to delve into the reasons behind it. Ca ...

JavaScript timekeepers and Ajax polling/scheduling

After looking into various methods like Comet and Long-Polling, I'm searching for a simpler way to push basic ajax updates to the browser. I've come across the idea of using Javascript timers to make Ajax calls at specific intervals. Is this app ...

The best way to consistently execute setTimeout in order, regardless of the timing, is by utilizing a Promise

I have the following code snippet: var timeOne = new Promise(function(resolve,reject) { setTimeout(function(){ console.log('This is one'); }, Math.random() * 1000); }); var timeTwo = new Promise(function(resolve,reject) { s ...

Stop Caching with Jquery Infinite Scroll (IAS)

I am using jQuery + IAS to implement infinite scroll functionality on my website. However, I have encountered an issue where the cache is being disabled when making requests for new content. Specifically, the URL that is accessed to retrieve the next page ...

Unveiling the Truth about Svelte Transitions

Recently, I attempted to incorporate the guidance provided in this repl () for implementing flying and fading effects on divs. However, it seems that I may have a slight misunderstanding regarding the concept... To tackle this, I designed a component rese ...

Using AngularJS to dynamically populate a dropdown menu from a JSON object

I am a beginner to Angular and currently facing my first significant challenge. The JSON object below is the address data retrieved from a third-party API and added to $scope.AddressData in a controller: $scope.AddressData = [{ "Address1":"South Row", ...

A guide on setting up ExpressJS Error Handling to display custom error messages based on the corresponding HTTP Status codes

I am struggling to figure out how to implement this task. You need to create an error on the /error path and pass it to the next function. Make sure to use appropriate error status codes for displaying different types of error messages. I attempted using ...

Utilize Vue.js 3 and InertiaJs to Retrieve Session Information in Laravel 9

I am currently working on my initial Laravel project, incorporating Vuejs for the frontend. One of the key features of my application is allowing a Super admin to log in as a User (impersonate). By clicking on the Impersonate Button, the word impersonate g ...

Using Unicode JSON in Laravel blade to pass data to React components, encountering an issue with JSON parsing

I currently have a JSON object stored in the database: { "ui": {}, "title": "Hola mundo 2", "values": {}, "properties": {}, "description": "descripcion" } Within the Laravel controller, ...

Exploring the world of handling GET and POST parameters in Node.js with

As someone who is new to Node/Express, I've noticed that GET parameters can be captured using the following syntax: app.get('/log/:name', api.logfunc); For POST requests, it can be done like this: app.post('/log', ... (with for ...

Securing the public webpack bundle access in ExpressJS?

Within my webpack configuration, the publicPath is defined as follows: publicPath: '/js' This setting directs it to public/js. Additionally, in my server-loaded index.pug file (not located within the public folder), I have the following code: ...

What are the steps for setting up node7 on a Mac operating system?

Currently facing an issue while trying to install node 7 or above on my Mac OS. I executed the following command: $brew install node Warning: node-7.9.0 is already installed, but not linked. Even though it indicates that node-7.9.0 is installed, when I c ...

Node version conflicts on macOS

Recently, I decided to switch over to Angular6 for my projects, but encountered an error message stating that my node version was outdated (lower than 8.9). After attempting to resolve this issue, it appears that my system is running two different versions ...

Sorting information in the React Native Section List

Working with React Native's SectionList and filtering data: data: [ { title: "Asia", data: ["Taj Mahal", "Great Wall of China", "Petra"] }, { title: "South America", data: ["Machu Picchu", "Christ the Redeemer", "C ...

Is MongoDB's find() method returning incorrect objects?

My current task involves running a Mongo query on the Order database to retrieve orders associated with a specific user by using their email. This process is achieved through an API, but I'm encountering difficulties as the returned object contains un ...

Having trouble integrating VueX store and router into Mocha tests

Latest Update To view the issue on VueX git repository that has been created, please follow this link: https://github.com/vuejs/vuex/issues/1509 If you want to replicate the problem, here is the link to the repository: https://github.com/djam90/vuex-vue- ...

Automating web pages with the power of Node.js

I have developed an API in PHP that accepts a 10-digit number as a variable and provides data in JSON format. For instance, the URL structure is like this: www.exampletest.com/index.php?number=8000000000. The succeeding variable will be the current number ...

What is the best method for setting up message scheduling for a Microsoft Teams bot based on different time

I am trying to figure out how to send messages to users from my bot at specific time intervals. Currently, I'm using agenda for scheduling messages, but I've run into an issue with the timezone discrepancy - agenda is 5:30 hours behind my timezon ...

Learn how to dynamically enable or disable the add and remove buttons in the PrimeNG PickList using Angular 2

I'm currently learning Angular 2 and I'm working on creating a dual list box using PrimeNG's pickList component (https://www.primefaces.org/primeng/#/picklist). Within the pickList, I have table data with 3 columns, along with ADD and REMO ...