-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
67 lines (58 loc) · 1.61 KB
/
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
let express = require('express');
let app = express();
let port = process.env.port || 3000;
require('./dbconnection');
let router = require('./routers/router');
/*const { MongoClient, ServerApiVersion } = require('mongodb');
const uri = "mongodb+srv://renurose2402:[email protected]/?retryWrites=true&w=majority";
let collection;*/
app.use(express.static(__dirname + '/'));
app.use(express.json());
app.use(express.urlencoded({extended: false}));
app.use('/api/cat',router);
/*const client = new MongoClient(uri, {
serverApi: {
version: ServerApiVersion.v1,
strict: true,
deprecationErrors: true,
}
});
async function runDB() {
try {
// Connect the client to the server (optional starting in v4.7)
await client.connect();
collection = client.db().collection('Cats');
console.log(collection);
} catch (ex) {
console.error(ex);
}
}
function insertCat(cat,callback) {
collection.insertOne(cat,callback);
}
function getAllCats(callback){
collection.find({}).toArray(callback);
}
app.get('/', function (req,res) {
res.render('index.html');
});
app.post('/api/cat', (req,res)=>{
let cat = req.body;
console.log(cat);
insertCat(cat, (err,result) => {
if(!err){
res.json({statusCode:201,data:result,message:'success'});
}
});
});
app.get('/api/cats', (req,res)=>{
getAllCats((err,result)=>{
console.log(result);
if(!err){
res.json({statusCode:200,data:result,message:'success'});
}
});
});*/
app.listen(port, ()=>{
console.log('express server started');
});