--- /dev/null
+FROM node:16-slim
+WORKDIR /app
+COPY ./package.json ./package.json
+RUN npm install --only=production
+COPY . .
+EXPOSE 80
+ENV PORT=80
+
+CMD [ "npm", "run", "start" ]
--- /dev/null
+{
+ "name": "cc",
+ "version": "1.0.0",
+ "lockfileVersion": 2,
+ "requires": true,
+ "packages": {
+ "": {
+ "version": "1.0.0",
+ "license": "ISC"
+ }
+ }
+}
--- /dev/null
+{
+ "name": "cc",
+ "version": "1.0.0",
+ "description": "",
+ "main": "src/index.js",
+ "scripts": {
+ "start": "node server.js",
+ "test": "node src/index.test.js"
+ },
+ "author": "",
+ "license": "ISC"
+}
--- /dev/null
+NAME=cc
+docker stop $NAME-container
+docker rm $NAME-container
+docker build -t $NAME-image .
+docker run -ditp $1:80 --name $NAME-container $NAME-image
+echo "\nRunning at http://localhost:$1/\n"
--- /dev/null
+const http = require("http");
+const { app } = require("./src");
+
+const handler = (request, response) => {
+ const { status = 200, body = "", headers = {} } = app(request);
+
+ response.writeHead(status, headers);
+ response.write(body);
+ response.end();
+};
+
+http.createServer(handler).listen(80);
+
+console.log("Node.js web server is running..");
--- /dev/null
+const redirectTo = ({ to }) => ({
+ status: 301,
+ headers: { location: to },
+});
+
+const match = (url1, url2) =>
+ url1 === url2 || url1 + "/" === url2 || url1 === url2 + "/";
+
+const redirects = [
+ { from: "/", to: "https://www.benlarson.xyz/" },
+ { from: "/so", to: "https://stackoverflow.com/users/10377586/ben-larson" },
+];
+
+module.exports = {
+ app: ({ url }) => {
+ const redirect = redirects.find(({ from }) => match(url, from));
+ if (redirect) return redirectTo(redirect);
+
+ return { status: 404 };
+ },
+};
--- /dev/null
+const { app } = require(".");
+
+const it = (assertion, callback) => {
+ try {
+ callback();
+ } catch (error) {
+ console.log(`\n${assertion}\n-> ${error}\n`);
+ }
+};
+
+const assertEqual = (actual, expected) => {
+ if (actual !== expected) throw `expected ${expected} but got ${actual}`;
+};
+
+it("returns 404", () => {
+ const { status } = app({ url: "/nothing-here" });
+
+ assertEqual(status, 404);
+});
+
+it("redirects to benlarson.xyz", () => {
+ const {
+ status,
+ headers: { location },
+ } = app({ url: "/" });
+
+ assertEqual(status, 301);
+ assertEqual(location, "https://www.benlarson.xyz/");
+});