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
![](https://learningaboutcode.com/wp-content/uploads/2020/12/Screenshot-2020-12-16-at-10.54.11-AM-1024x350.png)
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.
![](https://learningaboutcode.com/wp-content/uploads/2020/12/Screenshot-2020-12-16-at-11.04.48-AM-1024x556.png)
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.
![](https://learningaboutcode.com/wp-content/uploads/2020/12/Screenshot-2020-12-16-at-12.29.22-PM-1-1024x556.png)
Now, lets run our code by npm start.
Go to the browser and run localhost:9000, we will get the GET request result
![](https://learningaboutcode.com/wp-content/uploads/2020/12/Screenshot-2020-12-16-at-1.21.34-PM-1024x560.png)
Now, lets setup our db connection, for the POST method.
![](https://learningaboutcode.com/wp-content/uploads/2020/12/Screenshot-2020-12-16-at-2.30.53-PM-1024x584.png)
After, connecting to the mongodb, we will have the above code.
Now, lets add a modal for named as MessageModal, for defining its Schema.
![](https://learningaboutcode.com/wp-content/uploads/2020/12/Screenshot-2020-12-16-at-2.39.04-PM-1024x328.png)
Now, lets run in postman to test it. Before, getting into postman, I want to write the get method also.
![](https://learningaboutcode.com/wp-content/uploads/2020/12/Screenshot-2020-12-16-at-3.48.49-PM-1024x580.png)
Above is the complete code for GET/POST method.
![](https://learningaboutcode.com/wp-content/uploads/2020/12/Screenshot-2020-12-16-at-4.02.08-PM-1024x524.png)
![](https://learningaboutcode.com/wp-content/uploads/2020/12/Screenshot-2020-12-16-at-4.03.11-PM-1024x498.png)
This is my first post for node.js. Please feel free to provide feedback. I woul really appreciate.