GET/POST in nodejs

Node.js is an open-source, server-side, powerful JavaScript-based platform built on Google Chrome’s JavaScript v8 Engine.

Lets practice GET/POST/ in node.js

create a new folder

mkdir practiceNode
cd practiceNode

After navigating to practiceNode,

Lets a new file called server.js , which has all the setup.

let do npm init (can be used to set up a new or existing npm package) which helps to create package.json file.

Lets install the required dependencies for node

npm i express mongoose

express is a backend web application framework

Mongoose is an Object Data Modeling (ODM) library for MongoDB

https://www.mongodb.com/cloud/atlas -> get the database name and password for the connection of the DB.

In node.js, by dafault if we want to import something then we need to use require

const express = require express()

For this to change we can add “type”: “module” in package.json file.

Now, lets write some code in server.js file.

Now, lets run our code by npm start.

Go to the browser and run localhost:9000, we will get the GET request result

Now, lets setup our db connection, for the POST method.

After, connecting to the mongodb, we will have the above code.

Now, lets add a modal for named as MessageModal, for defining its Schema.

Now, lets run in postman to test it. Before, getting into postman, I want to write the get method also.

Above is the complete code for GET/POST method.

This is my first post for node.js. Please feel free to provide feedback. I woul really appreciate.

Leave a Reply

Your email address will not be published. Required fields are marked *