Advertisement
Guest User

Untitled

a guest
Apr 18th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.25 KB | None | 0 0
  1. const mongoDBConnectionString = "mongodb://bob:test@ds261917.mlab.com:61917/teams-api"
  2. const mongoDBUserConnectionString = "mongodb://bob:test!@ds247449.mlab.com:47449/users"
  3.  
  4. const HTTP_PORT = process.env.PORT || 8081;
  5.  
  6. const express = require("express");
  7. const bodyParser = require('body-parser');
  8.  
  9. const cors = require("cors");
  10. const dataService = require("./data-service.js");
  11. const dataServiceAuth = require("./data-service-auth.js");
  12.  
  13. const data = dataService(mongoDBConnectionString);
  14. const dataAuth = dataServiceAuth(mongoDBUserConnectionString);
  15. var jwt = require('jsonwebtoken');
  16. var passport = require("passport");
  17. var passportJWT = require("passport-jwt");
  18.  
  19. var ExtractJwt = passportJWT.ExtractJwt;
  20. var JwtStrategy = passportJWT.Strategy;
  21.  
  22. var jwtOptions = {};
  23. jwtOptions.jwtFromRequest = ExtractJwt.fromAuthHeaderWithScheme("jwt");
  24. jwtOptions.secretOrKey = '0La026@61Mm6';
  25.  
  26. var strategy = new JwtStrategy(jwtOptions, function (jwt_payload, next) {
  27. console.log ('payload received', jwt_payload);
  28. if (jwt_payload) {
  29. next(null, {_id: jwt_payload._id});
  30. } else {
  31. next(null,false);
  32. }
  33. });
  34. passport.use(strategy);
  35. const app = express();
  36. app.use(passport.initialize());
  37.  
  38. app.use(bodyParser.json());
  39. app.use(cors());
  40.  
  41. //Register & Loging Routes
  42.  
  43. app.post("/register",(req,res) => {
  44. dataAuth.registerUser(req.body).
  45. then((msg) => {
  46. res.json({"message": msg});
  47. }).catch((msg) => {
  48. res.status(422).json({"message": msg});
  49. });
  50. })
  51.  
  52. app.post("/login",(req,res) => {
  53. dataAuth.checkUser(req.body)
  54. .then((user) => {
  55. var payload = {
  56. _id: user._id,
  57. userName: user.userName,
  58. fullName: user.fullName,
  59. role: user.role
  60. };
  61. var token = jwt.sign(payload,jwtOptions.secretOrKey);
  62.  
  63. res.json({"message": "login successful", token: toekn});
  64.  
  65. }).catch((msg) => {
  66. res.status(422).json({"message": msg});
  67. })
  68. })
  69. // "Employee" Routes
  70.  
  71. app.get("/employees", (req,res) => {
  72. data.getAllEmployees().then((data)=>{
  73. res.json(data);
  74. })
  75. .catch((err)=>{
  76. res.status(500).end();
  77. })
  78. });
  79.  
  80. app.get("/employees-raw", (req,res) => {
  81. data.getAllEmployeesRaw().then((data)=>{
  82. res.json(data);
  83. })
  84. .catch((err)=>{
  85. res.status(500).end();
  86. })
  87. });
  88.  
  89. app.get("/employee/:employeeId", (req,res) => {
  90. data.getEmployeeById(req.params.employeeId).then((data)=>{
  91. if(data.length > 0){
  92. res.json(data);
  93. }else{
  94. res.status(404).end();
  95. }
  96. })
  97. .catch((err)=>{
  98. res.status(500).end();
  99. })
  100. });
  101.  
  102. app.get("/employee-raw/:employeeId", (req,res) => {
  103. data.getEmployeeByIdRaw(req.params.employeeId).then((data)=>{
  104. if(data.length > 0){
  105. res.json(data);
  106. }else{
  107. res.status(404).end();
  108. }
  109. })
  110. .catch((err)=>{
  111. res.status(500).end();
  112. })
  113. });
  114.  
  115. app.put("/employee/:employeeId", (req, res) => {
  116.  
  117. data.updateEmployeeById(req.params.employeeId, req.body).then((data)=>{
  118. res.json({"message": "Employee " + data + " updated successfully"});
  119. })
  120. .catch((err)=>{
  121. res.status(500).end();
  122. })
  123. });
  124.  
  125. app.post("/employees", (req, res) => {
  126.  
  127. data.addEmployee(req.body).then((data)=>{
  128. res.json({"message": "Employee " + data + " added successfully"});
  129. })
  130. .catch((err)=>{
  131. res.status(500).end();
  132. })
  133. });
  134.  
  135. ////////////////////
  136.  
  137. // "Position" Routes
  138.  
  139. app.get("/positions", (req,res) => {
  140. data.getAllPositions().then((data)=>{
  141. res.json(data);
  142. })
  143. .catch((err)=>{
  144. res.status(500).end();
  145. })
  146. });
  147.  
  148. app.get("/position/:positionId", (req,res) => {
  149. data.getPositionById(req.params.positionId).then((data)=>{
  150. if(data.length > 0){
  151. res.json(data);
  152. }else{
  153. res.status(404).end();
  154. }
  155. })
  156. .catch((err)=>{
  157. res.status(500).end();
  158. })
  159. });
  160.  
  161. app.put("/position/:positionId", (req,res) => {
  162. data.updatePositionById(req.params.positionId, req.body).then((data)=>{
  163. res.json({"message": "Position " + data + " updated successfully"});
  164. })
  165. .catch((err)=>{
  166. res.status(500).end();
  167. })
  168. });
  169.  
  170. app.post("/positions", (req, res) => {
  171.  
  172. data.addPosition(req.body).then((data)=>{
  173. res.json({"message": "Position " + data + " added successfully"});
  174. })
  175. .catch((err)=>{
  176. res.status(500).end();
  177. })
  178. });
  179.  
  180. ////////////////////
  181.  
  182. // "Project" Routes
  183.  
  184. app.get("/projects", (req,res) => {
  185. data.getAllProjects().then((data)=>{
  186. res.json(data);
  187. })
  188. .catch((err)=>{
  189. res.status(500).end();
  190. })
  191. });
  192.  
  193. app.get("/project/:projectId", (req,res) => {
  194. data.getProjectById(req.params.projectId).then((data)=>{
  195. if(data.length > 0){
  196. res.json(data);
  197. }else{
  198. res.status(404).end();
  199. }
  200. })
  201. .catch((err)=>{
  202. res.status(500).end();
  203. })
  204. });
  205.  
  206. app.put("/project/:projectId", (req,res) => {
  207. data.updateProjectById(req.params.projectId, req.body).then((data)=>{
  208. res.json({"message": "Project " + data + " updated successfully"});
  209. })
  210. .catch((err)=>{
  211. res.status(500).end();
  212. })
  213. });
  214.  
  215. app.post("/projects", (req, res) => {
  216.  
  217. data.addProject(req.body).then((data)=>{
  218. res.json({"message": "Project " + data + " added successfully"});
  219. })
  220. .catch((err)=>{
  221. res.status(500).end();
  222. })
  223. });
  224.  
  225. ////////////////////
  226.  
  227. // "Team Routes"
  228.  
  229. app.get("/teams", (req,res) => {
  230. data.getAllTeams().then((data)=>{
  231. res.json(data);
  232. })
  233. .catch((err)=>{
  234. res.status(500).end();
  235. })
  236. });
  237.  
  238. app.get("/teams-raw", (req,res) => {
  239. data.getAllTeamsRaw().then((data)=>{
  240. res.json(data);
  241. })
  242. .catch((err)=>{
  243. res.status(500).end();
  244. })
  245. });
  246.  
  247. app.get("/team/:teamId", (req,res) => {
  248. data.getTeamById(req.params.teamId).then((data)=>{
  249. if(data.length > 0){
  250. res.json(data);
  251. }else{
  252. res.status(404).end();
  253. }
  254. })
  255. .catch((err)=>{
  256. res.status(500).end();
  257. })
  258. });
  259.  
  260. app.get("/team-raw/:teamId", (req,res) => {
  261. data.getTeamByIdRaw(req.params.teamId).then((data)=>{
  262. if(data.length > 0){
  263. res.json(data);
  264. }else{
  265. res.status(404).end();
  266. }
  267. })
  268. .catch((err)=>{
  269. res.status(500).end();
  270. })
  271. });
  272.  
  273. app.put("/team/:teamId", (req,res) => {
  274. data.updateTeamById(req.params.teamId, req.body).then((data)=>{
  275. res.json({"message": "Team " + data + " updated successfully"});
  276. })
  277. .catch((err)=>{
  278. res.status(500).end();
  279. })
  280. });
  281.  
  282. app.post("/teams", (req, res) => {
  283.  
  284. data.addTeam(req.body).then((data)=>{
  285. res.json({"message": "Team " + data + " added successfully"});
  286. })
  287. .catch((err)=>{
  288. res.status(500).end();
  289. })
  290. });
  291.  
  292. ////////////////////
  293.  
  294. // Catch-All 404 error
  295.  
  296. app.use((req, res) => {
  297. res.status(404).end();
  298. });
  299.  
  300. // Connect to the DB and start the server
  301.  
  302. data.connect().then(()=>{
  303. app.listen(HTTP_PORT, ()=>{console.log("API listening on: " + HTTP_PORT)});
  304. })
  305. .catch((err)=>{
  306. console.log("unable to start the server: " + err);
  307. process.exit();
  308. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement