Skip to content

Instantly share code, notes, and snippets.

@khalid32
Last active September 23, 2018 17:29
Show Gist options
  • Save khalid32/ad096bc494dced17b5ed77456c4b36d6 to your computer and use it in GitHub Desktop.
Save khalid32/ad096bc494dced17b5ed77456c4b36d6 to your computer and use it in GitHub Desktop.
nodeJS example using Express...
// Requires Express, as before.
let express = require("express");
// Requires Morgan.
let morgan = require("morgan");
let app = express();
// Uses the Morgan middleware instead of the one you used to have.
let morganMiddleware = morgan("short");
app.use(morganMiddleware);
// Puts the static path in a variable.
let staticPath = path.join(__dirname, "static");
let statPath = express.static(staticPath);
// Uses express.static to serve files from the static path.
app.use(statPath);
app.use((req, res) => {
res.status(404);
res.send("File not found!");
});
app.listen(3000, () => {
console.log("App started on port 3000..");
});
// Requires all of the modules you need
let http = require("http");
let path = require("path");
let express = require("express");
let logger = require("morgan");
let bodyParser = require("body-parser");
// Make an express app
let app = express();
// the first line tells Express that the views are in the views folder; the next line says the views will use the EJS engine.
app.set("views", path.resolve(__dirname, "views"));
app.set("view engine", "ejs");
// Creates a global array to store all your entries.
let entries = [];
// Makes this entries array available in all views.
app.locals.entries = entries;
// Use Morgan to log every request
app.use(logger("dev"));
// Populates a variable called req.body if the user is submitting a form. (the extended option is required.)
app.use(bodyParser.urlencoded({ extended: false }));
// When visiting the site root, renders the homepage(at views/index.ejs)
app.get("/", (request, response) => {
response.render("index");
});
// Renders the "new entry" page (at views/index.ejs) when GETting the URL
app.get("/new-entry", (request, response) => {
response.render("new-entry");
});
// Defines a route handler when you POST to the "new-entry" URL in contrast to a GET
app.post("new-entry", (request, response) => {
// If user submits the form with no title or content, responds with a 400 error.
if(!request.body.title || !request.body.body){
response.status(400).send("Entries must have a title and a body.");
return;
}
// Adds a new entry to the list of entries.
entries.push({
title: request.body.title,
content: request.body.body,
published: new Date()
});
// Redirects to the homepage to see your new entry.
response.redirect("/");
});
// Renders a 404 page becaue you're requesting an unknown source.
app.use((request, response) => {
response.status(404).render("404");
});
// Starts the server on port 3000!
http.createServer(app).listen(3000, () => {
console.log("App started on port 3000");
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment