Node.
js Notes
Nodejs api // this is build in packages which is install with nodes js and are
helpful for making work easier
Fs //this is used to create a file and also write a masge inside it and send it to
user
[Link](“path_of_file”, “masage”, “callBack function”)
Os // this package will provide information about user machine
[Link]() // this will give me details about user
Lodash //this is used to perform operation on variable data , it is good to deal
with data
[Link]([]) //this will remove redundant data from given array
IsString(“Value”) // give true if data is string and falls if not
Creating a Server with express
Install express
Import it in file
Create a variable and store express functionality in it
Give a port address to run sever on it
Use different methods to get and post data to server
Import express form ‘express’ // improting
Const app = express();// assigning functionality
// do working hare
[Link](‘/address’, function(request,response){ [Link](‘Aslam o
Alaikum)}) // when we type this address to our browser then we will
receive this massage , Basically get method is used to get data form
server
….
[Link](portnumber) /// address of prot whare server will be run
Create database conection with node server
// import mongoes
import mongoose from "mongoose";
// 2 deffine the mongodb connection URL
const mongoUrl = "mongodb://localhost:27017/db_hostels";
// 3 set up MongoDb connection
[Link](mongoUrl, {
// there are some parameters which is necessory to pass
useNewUrlParser: true,
useUnifiedTopology:true
});
// get the default connection
// mongoose maintian a default connection object repersenting the
mongoDb connection
// this object is used to create every connection
// this object is also use to handle events and interact with the
database
const db= [Link];
// creating event listener
[Link]('connected',() =>{
[Link]("Connected to DataBase server");
});
[Link]('error',(err)=>{
[Link]('MongoDb connection error:',err);
});
[Link]('disconnected',()=>{
[Link]('MongoDb Disconnected');
});
// exprot database connection to connect
export default db;
Creating Models or schema of database collection
Body parser
Is a middle were , which is used to change data formet to suitable
format , understand by servers.
Express Router
There are lots of endpoint in a project and include all endpoint in single
server file make code readability is much difficult.
Express js provide feature of [Link] which help to manage
routes between different endpoint files