-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
73 lines (60 loc) · 1.65 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
73
const express = require("express");
const exphbs = require("express-handlebars");
const app = express();
const path = require("path")
const db = require("./db/connection");
const bodyParser = require("body-parser");
const job = require("./models/Job");
const Squelize = require("sequelize");
const Op = Squelize.Op;
const port = 3000;
app.listen(port, function() {
console.log(`O express está rodando na porta ${port}` );
});
// body parser
app.use(bodyParser.urlencoded({extended: false}));
// handlebars
app.set("views", path.join(__dirname, "views"));
app.engine("handlebars", exphbs.engine({defaultLayout: "main"}))
app.set("view engine", "handlebars");
// static folder
app.use(express.static(path.join(__dirname,"public")));
// db connection
db
.authenticate()
.then(() => {
console.log("Banco de dados conectado");
})
.catch(err => {
console.log("ocorreu um erro ao conectar ao banco de dados",err);
});
// routes
app.get("/", (req,res) => {
let search = req.query.job;
let query = "%"+search+"%";
if(!search){
job.findAll({order: [
["createdAt", "DESC"]
]})
.then(jobs => {
res.render("index",{
jobs
});
})
.catch(err => console.log(err))
}else {
job.findAll({
where:{title: {[Op.like]:query}},
order: [
["createdAt", "DESC"]
]})
.then(jobs => {
res.render("index",{
jobs, search
});
})
.catch(err => console.log(err))
}
});
// jobs routes
app.use("/jobs", require("./routes/jobs"));