-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
67 lines (58 loc) · 2.06 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
const express = require('express');
const mongoose = require('mongoose');
const dotenv = require('dotenv');
const mainRoutes = require('./routes/main');
const tch_routePost = require('./routes/reactRoute/tch_route');
// const callTestApi = require('./TEST/CALL-API-SERVER/api-server');
const cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
const path = require('path');
// const cors = require("cors");
const app = express();
// post of client
// var corsOptions = {
// origin: "http://localhost:8082/"
// };git
// app.use(cors(corsOptions));
// app.use(cors());
app.use(cookieParser());
// for parsing application/json
app.use(bodyParser.json());
// for parsing application/xwww-
app.use(bodyParser.urlencoded({ extended: true }));
dotenv.config({ path: "./config.env" });
// connet database
mongoose.connect('mongodb+srv://hoa:[email protected]/be1?retryWrites=true&w=majority', { useNewUrlParser: true, useUnifiedTopology: true });
// // LOCAL HOST
// const url = 'mongodb://127.0.0.1:27017/be1';
// mongoose.connect(url, { useNewUrlParser: true })
// thong bao ket noi thanh cong or not
const db = mongoose.connection;
db.on('error', console.error.bind(console, 'DB connection error:'));
db.once('open', function () {
console.log('DB connection successfully!');
});
app.set('view engine', 'pug');
app.set('view engine', 'ejs');
// serving static files
app.use(express.static(`${__dirname}/public`));
//để có thể nhận được dữ liệu tu req.body.
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
// uu tien client chay truoc
app.use(express.static('client/build'));
app.get('/', (req, res) => {
res.sendFile(path.resolve(__dirname, 'client', 'build', 'index.html'))
});
app.use(mainRoutes);
// api server data
app.use(tch_routePost);
app.all('*', (req, res, next) => {
res.status(200).render('page404.pug', {
title: 'Page not found!'
});
});
const port = process.env.PORT || 5000;
app.listen(port, function () {
console.log('App listening on port ' + port);
});