Skip to content

Instantly share code, notes, and snippets.

@code-boxx
Created June 9, 2023 07:02
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 code-boxx/3a590727ba58fbdeaa59c6123b78b5eb to your computer and use it in GitHub Desktop.
Save code-boxx/3a590727ba58fbdeaa59c6123b78b5eb to your computer and use it in GitHub Desktop.
NodeJS HTTP Server

NODEJS HTTP SERVER

https://code-boxx.com/simple-http-web-server-nodejs/

NOTES

  1. With the http module:
    • Install the required modules - npm i mime-types
    • Run 1a-http.js and access http://localhost/ in your browser.
  2. express HTTP server:
    • Install the required modules - npm i express
    • Create a public folder, move 2c-demo.css inside.
    • Run 2a-express.js and access http://localhost/ in your browser.
  3. http-server - See 3-http-server.txt.

LICENSE

Copyright by Code Boxx

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

// (A) LOAD REQUIRED MODULES
// npm i mime-types
const http = require("http"),
path = require("path"),
fs = require("fs"),
mime = require("mime-types");
// (B) HTTP SERVER
const server = http.createServer((req, res) => {
// (B1) SERVE HTML HOME PAGE
if (req.url == "/") {
let file = path.join(__dirname, "/1b-index.html");
res.writeHead(200, { "Content-Type": mime.lookup(file) });
res.write(fs.readFileSync(file));
}
// (B2) NOT FOUND
else {
res.writeHead(404, { "Content-Type": "text/html" });
res.write("<html><body>Not Found</body></html>");
}
res.end();
});
// (C) LISTEN TO PORT 80
server.listen(80);
console.log("Server is running!");
<!DOCTYPE html>
<html>
<head>
<title>Demo Page</title>
<meta charset="utf-8">
</head>
<body>
<h1>DEMO PAGE</h1>
<p>It works!</p>
</body>
</html>
// (A) LOAD REQUIRED MODULES
// npm i express
const express = require("express"),
path = require("path");
// (B) INIT EXPRESS SERVER
const app = express();
// (C) SERVE STATIC FILES + HOME PAGE
app.use("/public", express.static(path.join(__dirname, "public")))
app.get("/", (req, res) => res.sendFile(path.join(__dirname, "/2b-index.html")));
// (D) START SERVER
app.listen(80, () => console.log(`Server running at port 80`));
<!DOCTYPE html>
<html>
<head>
<title>Demo Page</title>
<link rel="stylesheet" href="public/2c-demo.css">
<meta charset="utf-8">
</head>
<body>
<h1>DEMO PAGE</h1>
<p>It works!</p>
</body>
</html>
* {
font-family: Arial, Helvetica, sans-serif;
box-sizing: border-box;
}
1) Install http-server globally - "npm i http-server -g"
2) Start the server - "http-server"
3) Full documentation - https://www.npmjs.com/package/http-server
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment