-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
53 lines (38 loc) · 1007 Bytes
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
var fs = require('fs');
const express=require("express");
const { json } = require('express');
const app=express()
app.use(express.json());
let studentArray=[];
app.listen(3000);
app.get('/getStudents',(req,res)=>
{
res.json(studentArray);
fs.readFile("Users.json",function(err,data)
{
const users=JSON.parse(data);
console.log(users);
})
})
app.post('/addDetails',(req,res)=>
{
const{studentFirstName,collegeName,location}=req.body;
console.log(studentFirstName);
console.log(collegeName);
console.log(location);
res.json({msg:"added"});
const users=require("./Users.json");
let student={
StudentFirstName:studentFirstName,
CollegeName:collegeName,
Location:location,
}
//adding new data to user object
users.push(student);
//writing to a file
fs.writeFile("Users.json",JSON.stringify(users),err=>
{
if(err) throw err;
console.log("Done writing");
})
})