Skip to content

Commit

Permalink
test node structure
Browse files Browse the repository at this point in the history
  • Loading branch information
ctkcoding committed Apr 19, 2022
1 parent a9dbb17 commit e58658b
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 0 deletions.
7 changes: 7 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
let express = require("express");
let app = express();

// Here, we will require the configuration files
require("./config/static-files")(express, app);

module.exports = app;
2 changes: 2 additions & 0 deletions assets/test.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<h1>Hello there!</h1>
<p>If you can see this, the server is running and has been configured correctly.</p>
47 changes: 47 additions & 0 deletions bin/www
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#!/usr/bin/env node
let app = require("../app");
let http = require("http");

// Define a port for the server to listen on
let port = process.env.PORT || 3000;
app.set("port", port);

// Create a server instance
let server = http.createServer(app);

// Make the server listen on a port
server.listen(port);

// Handle errors and success
server.on("error", onError);
server.on("listening", onListening);

function pipeOrPort(address) {
return typeof address == "string" ? `pipe ${address}` : `port ${address.port}`;
}

function onError(error) {
if (error.syscall != "listen") {
throw error;
}

let bind = pipeOrPort(server.address());

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;
}
}

function onListening() {
let bind = pipeOrPort(server.address());
console.log(`Listening on ${bind}`);
}
3 changes: 3 additions & 0 deletions config/static-files.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = function(express, app) {
app.use("/assets", express.static("assets"));
};
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
"scripts": {
"start": "node ./bin/www",
}

0 comments on commit e58658b

Please sign in to comment.