-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·41 lines (35 loc) · 1.77 KB
/
index.js
File metadata and controls
executable file
·41 lines (35 loc) · 1.77 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
require("express-group-routes"); //initiate group route
const express = require("express"); // initiate express module/dependency
const app = express(); //declare new variable
const port = process.env.PORT || 5000; // declare port server that will use
const bodyParser = require("body-parser"); // initiate body parser
const session = require("express-session");
const Auth = require("./controllers/auth");
app.use(bodyParser.json());
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*"); // update to match the domain you will make the request from
res.header("Access-Control-Allow-Headers", "*");
next();
});
const Cat = require("./controllers/categories"); //declare variable categories from controllers
const home = require("./controllers/home");
const user = require("./controllers/user");
//create the homepage route
app.get("/", (req, res) => {
//res means, response, and it send string "Hello Express!" to the API
res.send("Hello Express!");
});
app.group("/api/v1", router => {
router.get("/categories", Cat.index); //get all categories
router.post("/category", Cat.store); // create new category
router.get("/articles", home.index); // get all article
router.get("/posts", home.pop); // get latest article
router.get("/article/:id", home.show); // get detail article
router.post("/posting", home.store); // post new article
router.get("/category/:id/article", home.getCat); //get article by category
router.delete("/articles/:id", home.delete); //delete Article
router.post("/login", Auth.login); //login
router.get("/user/:id/articles", user.showAll) //show articles by person
});
//when this nodejs app executed, it will listen to defined port
app.listen(port, () => console.log(`Listening on port ${port}!`));