Who is the course for? | How long will it take? | Other stuff...
π https://remind.ist
Email future you, today...
...aka schedule an email to yourself in the future to remind you about something.
$ mkdir remind.ist && cd remind.ist
$ npm initDon't have Node.js installed? Get it through Node Version Manager (nvm):
https://github.com/nvm-sh/nvm#installation-and-update
// app.js
const run = () => {
  console.log('Run, pump those crazy legs!')
}
run()...and this course is over, thanks for watching!
Joking, obviously π
$ node app.js
Run, pump those crazy legs!
$ $ npm install nodemon --save-dev// package.json
{
  ...
  "scripts": {
    "start": "nodemon --inspect -e js,html,hbs,sql,css,scss app.js"
  }
  ...
}More about Nodemon:
$ npm run start// app.js
require('dotenv').config()
const { createServer } = require('http')
const PORT = process.env.PORT || 1234
const server = createServer((request, response) => {
  return response.end('Look, this is the response!')
})
server.listen(PORT, () => {
  console.log(`We are running the server on port ${PORT}`)
})# .env
PORT=1234$ npm install dotenv --save-devMore about Dotenv:
// app.js
const { createServer } = require('http')
const PORT = 1234
const server = createServer((request, response) => {
  return response.end('Look, this is the response!')
})
server.listen(PORT, () => {
  console.log(`We are running the server on port ${PORT}`)
})Documentation for Node's standard HTTP library:
You did awesome! ππ
HTTP server in only a couple lines of code!
// config/constants.js
exports.STATIC_EXTENSIONS = [
  'jpg',
  'jpeg',
  'gif',
  'png',
  'svg',
  'ico',
  'css',
  'js',
  'xml',
  'webmanifest',
  'txt',
  'html',
  'eot',
  'ttf',
  'woff',
]// lib/responder.js
const { open } = require('fs').promises
const { STATIC_EXTENSIONS } = require('../config/constants')
const serveStaticFile = async ({ file, extension, statusCode }, response) => {
  if (STATIC_EXTENSIONS.indexOf(extension) === -1) throw new Error('not_found')
  let fileHandle
  try {
    fileHandle = await open(`./public/${file}`, 'r')
    const staticFile = await fileHandle.readFile()
    return response.end(staticFile)
  } catch (error) {
    console.error(error)
    throw new Error('not_found')
  } finally {
    if (fileHandle) fileHandle.close()
  }
}
module.exports = serveStaticFile
// initialisers/http.js
if (!process.env.PORT) require('dotenv').config()
const { createServer } = require('http')
const serveStaticFile = require('../lib/responder')
const { PORT, APP_NAME } = process.env
module.exports = () => {
  const server = createServer(async ({ url }, response) => {
    const urlTokens = url.split('.')
    const extension = urlTokens.length > 1 ? `${urlTokens[urlTokens.length - 1].toLowerCase().trim()}` : false
    const isRoot = [ '', '/' ].indexOf(url) > -1
    const path = isRoot ? '/index.html' : url
    try {
      return await serveStaticFile({ file: path, extension: isRoot ? 'html' : extension }, response)
    } catch (error) {
      console.error(error)
      return await serveStaticFile({
        file: '/error.html',
        extension: 'html',
        statusCode: 500
      }, response)
    }
  })
  server.on('request', ({ method, url }) => {
    const now = new Date()
    console.info(`=> ${now.toUTCString()} β ${method} ${url}`)
  })
  server.listen(PORT, () => {
    console.log(`=> ${APP_NAME} running on port ${PORT}`)
  })
}
// lib/responder.js
// line 2, at the top:
const { lookup } = require('mime-types')
// line 15, inside serveStaticFile's try block:
const mime = lookup(extension)
if (!mime) throw new Error('not_found')
response.writeHead(statusCode || 200, {
  'Content-Type': mime
})$ npm install mime-types --saveMore about mime-types:
See you next time where we look at Dynamic content and templating.
Β
Please don't hesitate to contact me or leave feedback on the course:
Telegram channel: https://t.me/frameworkless
Twitter: @mtimofiiv
See today's code: https://github.com/frameworkless-js/remind.ist/tree/stage/1
See today's lesson running live: https://part1.remind.ist