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
Fs.appenfile(“path_of_file”, “masage”, “callBack function”)
Os // this package will provide information about user machine
Os.userInfo() // this will give me details about user
Lodash //this is used to perform operation on variable data , it is good to deal
with data
Lodesh.uniq([]) //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
App.get(‘/address’, function(request,response){ response.send(‘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
….
app.listen(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
mongoose.connect(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= mongoose.connection;
// creating event listener
db.on('connected',() =>{
console.log("Connected to DataBase server");
});
db.on('error',(err)=>{
console.log('MongoDb connection error:',err);
});
db.on('disconnected',()=>{
console.log('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 express.router which help to manage
routes between different endpoint files