-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
50 lines (43 loc) · 1.43 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
40
41
42
43
44
45
46
47
48
49
50
import express from "express";
import { APP_PORT, DB_URL } from "./config";
import errorHandler from "./middlewares/errorHandler";
const app = express();
import routes from "./routes";
import mongoose from "mongoose";
import path from "path";
import cors from "cors";
// Database connection
mongoose.connect(DB_URL, {
dbName: "rest-api",
useNewUrlParser: true,
useUnifiedTopology: true,
//useFindAndModify: false,
});
const db = mongoose.connection;
db.on("error", console.error.bind(console, "connection error:"));
db.once("open", () => {
console.log("DB connected...");
});
//global.appRoot = path.resolve(__dirname);
app.use(cors());
app.use(express.urlencoded({ extended: false }));
app.use(express.json());
app.use("/api", routes);
app.use("/uploads", express.static("uploads"));
app.use("/", (req, res) => {
res.send(`
<h1>Welcome to E-commerce Rest APIs</h1>
You may contact me <a href="https://codersgyan.com/links/">here</a>
Or You may reach out to me for any question related to this Apis: codersgyan@gmail.com
`);
});
app.use(errorHandler);
const PORT = process.env.PORT || APP_PORT;
app.listen(PORT, () => console.log(`Listening on port ${PORT}.`));
// import express from "express";
// import { APP_PORT } from "./config";
// const app = express();
// app.get("/", function (req, res) {
// res.send("<b>My</b> first express http server");
// });
// app.listen(APP_PORT, () => console.log(`Listening on port ${APP_PORT}.`));