Skip to content

Instantly share code, notes, and snippets.

@METACEO
Created October 27, 2020 04:27
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 METACEO/c36be956b8bcf9da9c926d584168a887 to your computer and use it in GitHub Desktop.
Save METACEO/c36be956b8bcf9da9c926d584168a887 to your computer and use it in GitHub Desktop.
index.js for aws-ec2 sites
const http = require('http');
let pageHits = 0;
const server = http.createServer((req, res) => {
switch (req.url) {
case '/api': {
res.setHeader("Content-Type", "application/json");
res.writeHead(200);
res.end(JSON.stringify({
now: Date.now(),
pageHits
}));
break;
}
default: {
pageHits++;
res.setHeader("Content-Type", "text/html");
res.writeHead(200);
res.end(indexHtmlGenerator());
break;
}
}
});
server.listen(8080);
const indexHtmlGenerator = () => `<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/tailwindcss/1.9.6/tailwind.min.css"/>
</head>
<body>
<div class="min-h-screen bg-gray-100 py-6 flex flex-col justify-center sm:py-12">
<div class="relative py-3 sm:max-w-xl sm:mx-auto">
<div class="absolute inset-0 bg-gradient-to-r from-teal-400 to-blue-400 shadow-lg transform -skew-y-6 sm:skew-y-0 sm:-rotate-6 sm:rounded-3xl"></div>
<div class="relative px-4 py-10 bg-white shadow-lg sm:rounded-3xl sm:p-20">
<div class="max-w-md mx-auto">
<div class="divide-y divide-gray-200">
<div class="py-8 text-base leading-6 space-y-4 text-gray-700 sm:text-lg sm:leading-7">
<p>Page hits: ${pageHits}</p>
</div>
<div class="pt-6 text-base leading-6 font-bold sm:text-lg sm:leading-7">
<p>Want to hit the api?</p>
<a href="/api" class="text-teal-600 hover:text-teal-700"> Go to /api → </a>
</div>
</div>
</div>
</div>
</div>
</div>
</body>
</html>`;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment