-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
39 lines (31 loc) · 2.77 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
if (process.env.NODE_ENV !== "production") {
// this is done because we want dotenv to only load in the development environment
require("dotenv").config();
}
const express = require("express");
const app = express(); // to get the app portion of express
const expressLayouts = require("express-ejs-layouts");
const indexRouter = require("./routes/index"); // this will tell the location of our routes
const authorsRouter = require("./routes/authors");
const booksRouter = require("./routes/books");
const bodyParser = require("body-parser");
const methodOverride = require("method-override"); // to handle put and delete request
// configuring our express application
app.set("view engine", "ejs"); // we want to set the view engine as ejs
app.set("views", __dirname + "/views"); // we need to tell where our views are going to come from. For that we need to set a folder where all of the different view are going to go for our server.
app.set("layout", "layouts/layout"); // we can tell how our layout files are going to be
// the idea behind the layour files is that every single file is going to be put inside of this layout file, so that we don't have to duplicate all the begining and ending html of the project such as header and footer
app.use(expressLayouts); // tell node that we want to use the expressLayouts
app.use(express.static("public")); // this will tell where our public files are going to be like, stylesheets, js files, images
app.use(bodyParser.urlencoded({ limit: "10mb", extended: false })); //here urlencoded() is used because we are sending these values via url to our server. limit is to set the maximum size of the file that our server can accept.
app.use(methodOverride("_method"));
const mongoose = require("mongoose");
mongoose.connect(process.env.DATABASE_URL, { useNewUrlParser: true }); // DATABASE_URL is being provided by the environment variable when using server, but for local use we need to create a .env file and specify the variable over there otherwise it will through error. Now the DATABASE_URL is in .env file
const db = mongoose.connection;
db.on("error", (error) => console.error(error)); // this is to check whether we are connected to our database
db.once("open", () => console.log("Connected to Mongoose")); // this is going to run only once when we are connected to mongoose
app.use("/", indexRouter);
app.use("/authors", authorsRouter); // this will automatically append /authors before all the routes from the author
app.use("/books", booksRouter);
app.listen(process.env.PORT || 3000); // this is going to pull the port from the enviornment variable, the server is going to tell what source it is listening to but for the development purposes we assign a port number by ourselves since the server is not telling anything, so we set the port to 3000
// yTCAF334bng8wnHq;