-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
1,324 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} |
Oops, something went wrong.