Skip to content

Commit

Permalink
done change to es6
Browse files Browse the repository at this point in the history
  • Loading branch information
vitvuive committed Aug 29, 2021
1 parent 0502a85 commit 31b3346
Show file tree
Hide file tree
Showing 9 changed files with 1,324 additions and 60 deletions.
41 changes: 0 additions & 41 deletions app.js

This file was deleted.

45 changes: 45 additions & 0 deletions app.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { default as express } from "express";
import { default as hbs } from "hbs";
import * as path from "path";
// import * as favicon from 'serve-favicon';
import { default as logger } from "morgan";
import { default as cookieParser } from "cookie-parser";
import { default as bodyParser } from "body-parser";
import * as http from "http";
import { approotdir } from "./approotdir.mjs";
const __dirname = approotdir;
import {
normalizePort,
onError,
onListening,
handle404,
basicErrorHandler,
} from "./appsupport.mjs";
import { router as indexRouter } from "./routes/index.mjs";
// import { router as notesRouter } from './routes/notes.mjs';

export const app = express();
// view engine setup
app.set("views", path.join(__dirname, "views"));
app.set("view engine", "hbs");
hbs.registerPartials(path.join(__dirname, "partials"));
// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger("dev"));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, "public")));
// Router function lists
app.use("/", indexRouter);
// app.use('/notes', notesRouter);
// error handlers
// catch 404 and forward to error handler
app.use(handle404);
app.use(basicErrorHandler);
export const port = normalizePort(process.env.PORT || "3000");
app.set("port", port);
export const server = http.createServer(app);
server.listen(port);
server.on("error", onError);
server.on("listening", onListening);
5 changes: 5 additions & 0 deletions approotdir.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import * as path from "path";
import * as url from "url";
const __filename = url.fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
export const approotdir = __dirname;
57 changes: 57 additions & 0 deletions appsupport.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { port } from "./app.mjs";
import { server } from "./app.mjs";

export function normalizePort(val) {
const port = parseInt(val, 10);
if (isNaN(port)) {
return val;
}
if (port >= 0) {
return port;
}
return false;
}

export function onError(error) {
if (error.syscall !== "listen") {
throw error;
}
const bind = typeof port === "string" ? "Pipe " + port : "Port " + port;
switch (error.code) {
case "EACCES":
console.error(`${bind} requires elevated privileges`);
process.exit(1);
break;
case "EADDRINUSE":
console.error(`${bind} is already in use`);
process.exit(1);
break;
default:
throw error;
}
}

export function onListening() {
const addr = server.address();
const bind = typeof addr === "string" ? "pipe " + addr : "port " + addr.port;
console.log(`Listening on ${bind}`);
}

export function handle404(req, res, next) {
const err = new Error("Not Found");
err.status = 404;
next(err);
}
export function basicErrorHandler(err, req, res, next) {
// Defer to built-in error handler if headersSent
// See: http://expressjs.com/en/guide/error-handling.html
if (res.headersSent) {
return next(err);
}
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get("env") === "development" ? err : {};
// render the error page
res.status(err.status || 500);
res.render("error");
}
Loading

0 comments on commit 31b3346

Please sign in to comment.