-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
142 lines (130 loc) · 3.34 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// require("dotenv").config();
// import dotenv from "dotenv";
// dotenv.config();
import {config} from "dotenv";
config();
// const userLib = require("./backend/lib/userLib");
import * as userLib from "./backend/lib/userLib.js";
// const todoLib = require("./backend/lib/todoLib");
import * as todoLib from "./backend/lib/todoLib.js";
// const mongoose = require("mongoose");
import mongoose from "mongoose";
// const express = require('express');
import express,{request} from "express";
// const { request } = require("express");
const app = express();
const port = process.env.PORT || 5010;
const options = {
extensions:['htm','html','css','js','ico','jpg','jpeg','png','svg','pdf'],
index:['index.html'],
}
app.use(express.static("frontend"));
app.use(express.json());
app.use(express.static("public",options));
app.get("/api/todos",function(req,res){
todoLib.getAllTodos(function(err, todos){
if(err){
res.json({status: "error", message: err, data: null});
}
else{
res.json({status: "success", data: todos});
}
});
});
app.post("/api/todos", function(req, res){
const todo = req.body;
todoLib.createTodo(todo, function(err, dbtodo){
if(err){
res.json({status: "error", message: err, data: null});
}
else{
res.json({status: "success", data: dbtodo});
}
});
});
app.get("/api/todos/completed",function(req,res){
todoLib.getAllCompletedTodos(function(err, todos){
if(err){
res.json({status: "error", message: err, data: null});
}
else{
res.json({status: "success", data: todos});
}
});
});
app.get("/api/todos/deleted",function(req,res){
todoLib.getAllDeletedTodos(function(err, todos){
if(err){
res.json({status: "error", message: err, data: null});
}
else{
res.json({status: "success", data: todos});
}
});
});
app.put(("/api/todos/:todoid"),function(req,res){
const todo = req.body;
const todoid = req.params.todoid;
todoLib.updateTodoById(todoid, todo, function(err, dbtodo){
if(err){
res.json({status: "error", message: err, data: null});
}
else{
res.json({status: "success", data: dbtodo});
}
});
});
app.delete(("/api/todos/:todoid"),function(req,res){
const todoid = req.params.todoid;
todoLib.deleteTodoById(todoid, function(err,dbtodo){
if(err){
res.json({status: "error", message: err, data: null});
}
else{
res.json({status: "success", data: dbtodo});
}
});
});
app.get("/", function(req, res){
res.sendFile(process.cwd()+"/index.html");
});
app.get("/resume", function(req, res){
res.sendFile(process.cwd()+"/resume.html");
});
app.get("/card", function(req, res){
res.sendFile(process.cwd()+"/card/card.html");
});
app.get("/weather", function(req, res){
res.sendFile(process.cwd()+"/weather.html");
});
app.get("/todo", function(req, res){
res.sendFile(process.cwd()+"/frontend/html/todo.html");
});
mongoose.set('strictQuery', true);
mongoose.connect(process.env.MONGO_CONNECTION_STRING,{},function(err){
if(err){
console.error(err);
}
else{
console.log("DB Connected");
// userLib.createFirstUser(function(err,res){
// if(err){
// console.error(err);
// }
// else{
// console.log(res);
// }
// });
// userLib.getAllUsers(function(err,res){
// if(err){
// console.log(err);
// }
// else{
// console.log(res);
// }
// });
app.listen(port, function(){
console.log("Server running on http://localhost:"+port);
});
}
});