-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
57 lines (51 loc) · 1.57 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
51
52
53
54
55
56
57
// packages
import express, { urlencoded, json } from "express";
const app = express();
import mongoose from "mongoose";
import morgan from "morgan";
import { config } from "dotenv";
import cors from "cors";
import expressListRoutes from "express-list-routes";
import CarbonRouter from "./resources/carbon/carbonRouter";
import QRCodeRouter from "./resources/qrcode/qrcodeRouter";
import UltrasensorRouter from './resources/ultrasensor/ultrasensorRouter';
import upload from './resources/upload';
config();
const PORT = process.env.PORT || 3000;
app.use(json());
app.use(urlencoded({ extended: true }));
app.use(cors());
app.use(morgan("dev"));
//endpoint shows Server Running
app.get("/", (req, res) => {
res.json("Server is Running");
});
app.use("/api/v1/carbondata/", CarbonRouter);
app.use("/api/v1/qrcode/", QRCodeRouter);
app.use("/api/v1/ultrasensor-data/", UltrasensorRouter);
//util single file upload API
app.post("/upload", upload.single('image'), async (req, res) => {
if (!req.file) {
res.status(401).send("No Image found to upload");
}
res.status(201).send({ imageUrl: req.file.location });
});
export const start = async () => {
try {
mongoose.connect(process.env.MONGO_CONNECTION_STRING, {
useUnifiedTopology: true,
useNewUrlParser: true,
useFindAndModify: false,
useCreateIndex: true,
});
app.listen(PORT, () => {
if (process.env.NODE_ENV === "development") {
expressListRoutes(app);
}
console.log(`REST API on http://localhost:${PORT}/`);
});
} catch (e) {
console.error(e);
}
};
//P139yx3Etzi5sIDO