Node js
const file = require('fs');
const result = file.writeFileSync("hello.txt", "Hello my name is aditya");
console.log(result);
//hello.js
console.log("hello");
const math = require("./math");
//console.log(math.add(3,5));
const value =math.add(3,5);
console.log(value);
//maths export
function add(a , b){
return a + b;
}
function sub(a,b ){
return a-b;
}
//we have to export functions to use them in other files because we run the server with only one file
module.exports = {add,sub};
//server.js
const http = require("http");
const fs = require('fs');
const myserver = http.createServer((req, res)=>{
const log = `${Date.now()} : ${req.url} :The request recieved\n`;
fs.appendFile('serverlog.txt', log, (req, res)=>{ })
// console.log("request recieved");
switch(req.url){
case "./":
res.write('this is home page');
break;
case "/about":
res.write("This is about page");
break;
case "/contact":
res.write("this is contact page");
break;
}
// res.end(" hello everyone ");
});
myserver.listen(9000, ()=>{
console.log("server has started at port 9000");
})
//project 01
const express = require('express');
const app = express();
const users = require('./mock-data.json');
const port = 3000;
app.use(express.json()); // For parsing application/json
app.use(express.urlencoded({ extended: true }));
app.get('/users', (req,res)=>{
return res.json(users);
})
app.get('/users/:id', (req,res)=>{
const id = Number(req.params.id);
const user = users.find(user => user.id === id);
return res.json(user);
})
app.route('/users/:id').patch((req, res) => {
//update the user data with given id
return res.json({messge: "The id is being updated"});
}).delete((req, res) => {
//delete the user data with given id
return res.json({messge: "The id is being deleted"});
}).post((req,res)=>{
return res.send("The post request is in pending");
const newdata = req.body;
console.log(newdata);
return res.send("wait");
// users.push({...body, body:id= users.length+1 })
}
)
app.listen(port, () => {
console.log(`Server is running on http://localhost:${port}`);
});
Comments
Post a Comment