-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
72 lines (66 loc) · 2.85 KB
/
app.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
68
69
70
71
72
const blogRoute = require('./routes/routesBlogs');
const express = require('express');
const request = require('request');
//morgan http requester logger.
//const morgan = require('morgan');
const mongoose = require('mongoose'); //Mongoose is a MongoDB object modeling tool designed to work in an asynchronous environment. Mongoose supports both promises and callbacks.
require('dotenv').config();
//create express app
const app = express();
// we will use ejs as view engine.
app.set('view engine', 'ejs'); //by default look for ejs files in views dir
mongoose.connect(process.env.dbMongo, {
dbName: process.env.DB_NAME,
user: process.env.DB_USER,
pass: process.env.DB_PASS,
useNewUrlParser: true,
useUnifiedTopology: true,
useFindAndModify: false,
useCreateIndex: true,
useNewUrlParser: true, useUnifiedTopology: true }) //asyn task
.then((result) => {app.listen("3000"), console.log('the DB is now connected and app is running!!')})
.catch((err) => console.log("error connecting to DB", err));
//app.use(morgan('dev'));
//use express midlware & static files"pubish any files to the browser within public folder.
app.use(express.static('public/'));
//the below midlware to take the data from FE and pass it to the request object.
app.use(express.urlencoded({ extended: true}));
app.get('/', (req, res) => {
/* const blogs = [
{title: 'Yoshi finds eggs', snippet: 'Lorem ipsum dolor sit amet consectetur'},
{title: 'Mario finds stars', snippet: 'Lorem ipsum dolor sit amet consectetur'},
{title: 'How to defeat bowser', snippet: 'Lorem ipsum dolor sit amet consectetur'},
]
//res.sendFile('./views/index.html', {root: __dirname});
// send has 2 benfits: automatically set the header and send the status for us.
res.render('index', {title: 'Home', blogs }); */
res.redirect('/blogs');
});
//blog routes
///////////////////////////////////
app.use('/blogs', blogRoute);
//////////////////////////
// render
app.get('/about', (req, res) => {
res.render('about', {title: 'About'});
});
app.get('/serviceb/get', (req, res) => {
res.render('serviceb', {title: 'ServiceB'});
});
app.get('/serviceb/getusers', (req, res) => {
request('http://serviceb-svc.serviceb:80/users', function (error, response, body) {
console.log('error:', error); // Print the error if one occurred and handle it
console.log('statusCode:', response && response.statusCode); // Print the response status code if a response was received
//res.send(body)
res.render('getusers', {title: 'All GetUsers', users: body });
});});
//redirects
app.get('/about-me', (req, res) => {
res.redirect('/about');
});
//404 status : not found//
//in express we use : use and must be exists on the last
app.use((req, res) => {
res.status(404).render('404', {title: "Error!!"});
})
//the code run from top to buttom.404 page has to be at the buttom