-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
62 lines (54 loc) · 1.76 KB
/
Copy pathapp.js
File metadata and controls
62 lines (54 loc) · 1.76 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import express from "express";
import path from "path";
import bodyParser from "body-parser";
import rootDir from "./utils/path.js";
import multer from "multer";
import flash from "connect-flash";
import { router as categoryRouter} from "./routes/categoryRoutes.js";
import { router as productRouter} from "./routes/productRoutes.js";
import { sequelize } from "./utils/database.js";
import { Product } from "./models/product.js";
import { Category } from "./models/category.js";
import { seedCategories } from "./seeders/categorySeeder.js";
import { throw404 } from "./controllers/errors.js";
const app = express();
const fileStorage = multer.diskStorage({
destination:(req,file,cb)=>{
cb(null,"images");
},
filename:(req,file,cb)=>{
cb(null,Date.now()+"-"+file.originalname);
}
});
const fileFilter = (req,file,cb)=>{
if(file.mimetype === "image/png" || file.mimetype === "image/jpg" || file.mimetype === "image/jpeg"){
cb(null,true);
}
else{
cb(null,false);
}
};
app.set("view engine","ejs");
app.set("views","views")
app.use(bodyParser.urlencoded({extended:false}));
app.use(multer({storage:fileStorage,fileFilter:fileFilter}).single("image"));
app.use(express.static(path.join(rootDir,"public")));
app.use("/images",express.static(path.join(rootDir,"images")));
app.use(flash());
app.use("/categories",categoryRouter);
app.use("/products",productRouter);
app.use(throw404);
app.use((err,req,res,next)=>{
console.log(err);
res.status(500).render("500",{pageTitle:"Error!",path:"/500"});
})
Product.belongsTo(Category);
Category.hasMany(Product);
sequelize.sync({force:false})
.then(()=>{
return seedCategories();
})
.then(()=>{
app.listen(3000);
})
.catch(err=>{console.log(err);})