Skip to content

Instantly share code, notes, and snippets.

@muhsin-k
Created April 11, 2018 05:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save muhsin-k/104f6a887431ec83f2b57c2a6b8ef826 to your computer and use it in GitHub Desktop.
Save muhsin-k/104f6a887431ec83f2b57c2a6b8ef826 to your computer and use it in GitHub Desktop.
URL-Shortener
const express = require("express");
const mongoose = require("mongoose");
const bodyParser = require("body-parser");
const mongoURI = "mongodb://localhost/url-shortner";
const connectOptions = {
keepAlive: true,
reconnectTries: Number.MAX_VALUE
};
//Connect to MongoDB
mongoose.Promise = global.Promise;
mongoose.connect(mongoURI, connectOptions, (err, db) => {
if (err) console.log(`Error`, er);
console.log(`Connected to MongoDB`);
});
const app = express();
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Methods", "GET,PUT,POST,DELETE");
res.header(
"Access-Control-Allow-Headers",
"Content-type,Accept,x-access-token,X-Key"
);
if (req.method == "OPTIONS") {
res.status(200).end();
} else {
next();
}
});
app.use(bodyParser.json());
require("./models/UrlShorten");
require("./routes/urlshorten")(app);
const PORT = 7000;
app.listen(PORT, () => {
console.log(`Server started on port`, PORT);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment