This repository was archived by the owner on Feb 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 260
Expand file tree
/
Copy pathapp.js
More file actions
55 lines (43 loc) · 1.56 KB
/
app.js
File metadata and controls
55 lines (43 loc) · 1.56 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
// ADDING Modules
const express = require('express');
const path = require("path");
const bodyparser = require("body-parser");
const app = express();
// Specify port
const port = 80;
// _______MONGOOSE______
const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/test', { useNewUrlParser: true, useUnifiedTopology: true });
// Define mongoose schema
const reviewsSchema = new mongoose.Schema({
customername: String,
productname: Number,
reviews: String,
});
const Reviews = mongoose.model('Reviews',reviewsSchema);
// EXPRESS SPECIFIC STUFF
app.use('/static', express.static('static')); // For serving static files
app.use(express.urlencoded());
// TEMPLATE ENGINE SPECIFIC STUFF
app.set('view engine', 'jsx'); // set the view engine as jsx
app.set('views', path.join(__dirname,'/views')); // set the views directory
app.engine('jsx', require('express-react-views').createEngine());
// ENDPOINTS
app.get('/', (req,res)=>{
const params = {title: 'Customer Reviews'}
res.status(200).render('index',params);
});
app.post('/', (req,res)=>{
const mydata = new Reviews(req.body);
console.log(mydata);
const pra = {message: 'Thanks for giving your reviews, Your reviews are submitted.'}
mydata.save().then(()=>{
res.render('submitted',pra);
}).catch(()=>{
res.status(400).send(" Sorry there's a problem, Item was not saved to the database");
});
});
//START THE SERVER
app.listen(port,()=>{
console.log(`The application is running on the port ${port}`);
});