Skip to content

Commit

Permalink
server running
Browse files Browse the repository at this point in the history
  • Loading branch information
Zemotacqy committed Oct 25, 2019
1 parent ceff380 commit 5d2bb46
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/node_modules
package-lock.json
78 changes: 78 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
const express = require("express");
const app = express();
const bodyParser = require("body-parser");
const morgan = require("morgan");
const mongoose = require("mongoose");
const cors = require("cors");

/**
* Database connectivity
*/
const localurl = "mongodb://127.0.0.1/moscow"
mongoose
.connect(localurl, { useNewUrlParser: true }, (err, db) =>{
if(err){
console.log(err);
console.log("Database Connectivity Error!!");
} else {
console.log("Database Connectivity Successfull!");
}
})
mongoose.Promise = global.Promise;


/**
* Using MiddleWares
*/
app.use(morgan("dev"));
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
// handling cors errors
app.use(cors());


/**
* Routes
*/
// const rootRoutes = require('./routes/index.js');

// app.use('/api', rootRoutes);




// Handling the errors
/**
* If none of the above middlewares are hit there is definately an error
*/
app.use((req, res, next) => {
// create a new error object(inbuilt)
const error = new Error("Not Found!!");
error.status = 404;
// call the universal error handler and pass it to the next middleware this error
// Till here it was sure that a wrong url has been hit.
next(error);
});

/**
* Handling universal errors like dberror or server error etc
* we'll have a error function from the previous middleware || from the universal error
* This route is bound to hit
*/
app.use((error, req, res, next) => {
res.status(error.status || 500);
res.json({
"error": {
"message": error.message
}
});
});



// get the port no.
const port = 5000 || process.env.PORT;

app.listen(port, "localhost", ()=>{
console.log("Server running at port " + port);
});
30 changes: 30 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"name": "hack-moscow",
"version": "1.0.0",
"description": "Our team project for Hack.Moscow",
"main": "index.js",
"scripts": {
"start" : "nodemon index.js"
},
"repository": {
"type": "git",
"url": "git+https://github.com/Zemotacqy/hack-moscow.git"
},
"keywords": [
"Hack.Moscow"
],
"author": "Zemotacqy",
"license": "ISC",
"bugs": {
"url": "https://github.com/Zemotacqy/hack-moscow/issues"
},
"homepage": "https://github.com/Zemotacqy/hack-moscow#readme",
"dependencies": {
"body-parser": "^1.19.0",
"cors": "^2.8.5",
"express": "^4.17.1",
"mongoose": "^5.7.7",
"morgan": "^1.9.1",
"nodemon": "^1.19.4"
}
}
Empty file added routes/index.js
Empty file.

0 comments on commit 5d2bb46

Please sign in to comment.