-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
56 lines (45 loc) · 1.15 KB
/
app.js
File metadata and controls
56 lines (45 loc) · 1.15 KB
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
const express = require('express');
const bodyParser = require('body-parser');
const mongoose = require('mongoose');
let mongoUrl = `mongodb://localhost:27017/catalog`; // local mongo
mongoose.connect(mongoUrl)
.then(() => {
console.log("Connected to Database");
}).catch((err) => {
console.log("Not Connected to Database ERROR! ", err);
});
const app = express();
app.get("/", (req, res, next) => {
return res.send("Hello from node");
});
app.use(bodyParser.json());
const Schema = mongoose.Schema;
const productSchema = new Schema({
name: {
type: String
},
qty: {
type: Number
}
});
const product = mongoose.model('Product', productSchema);
app.post("/", (req, res, next) => {
const products = new product(req.body);
console.log(req.body);
products
.save()
.then(product => {
res.status(201).json({
success: true,
code: 201,
data: product
});
})
.catch(err => {
console.log("failed to create");
res.status(400).send({success: false});
});
});
app.listen(3000, () => {
console.log(`Server listen from 3000.....`);
});