20241211 ◎

Node.js

I misunderstood the purpose of Node.js.

Node.js - Introduction to Node.js

Node.js is an open-source and cross-platform JavaScript runtime environment.

Node.js has a unique advantage because millions of frontend developers that write JavaScript for the browser are now able to write the server-side code in addition to the client-side code without the need to learn a completely different language.

(copied from the documentation)

const { createServer } = require('node:http');

const hostname = '127.0.0.1';
const port = 3000;

const server = createServer((req, res) => {
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/plain');
  res.end('Hello World');
});

server.listen(port, hostname, () => {
  console.log(`Server running at http://${hostname}:${port}/`);
});

I have seen that Node.js is used as a development server that serves static files like HTML, CSS, and JavaScript directly to the browser.

When deploying the frontend client, I thought Node.js was not required because once static files are built, they can be hosted anywhere such as CDN.

However, as outlined in the documentation, Node.js provides the capability to create API endpoints, whch can be deployed on a Node.js server. This functionality is crucial for modern frameworks like Next.js, enabling them to deliver pure static files while leveraging a separate server for performance optimization. I’ve also heard that many frontend developers are using TypeScript for building backend APIs because of its shallow learning curve (for frontend developers).


_ lb


MUST:


index 20241210 20241212