nodejs ====== (Updated 2024.) Node or node.js or nodejs is a general purpose programming platform for JavaScript, often used for server-side web development. Node used Google's V8 JavaScript engine and some components written in C; its performance is good. On Debian: # apt-get install nodejs npm npm is the node package manager. Node has a REPL, invoked with: ``` $ nodejs > .help .break Sometimes you get stuck, this gets you out .clear Alias for .break .exit Exit the repl .help Show repl options .load Load JS from a file into the REPL session .save Save all evaluated commands in this REPL session to a file > ``` A simple nodejs server: ``` const http = require('http'); const server = http.createServer( function (req, res) { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello, World!\n'); } ) server.listen(9090, "127.0.0.1"); console.log('Server running at http://127.0.0.1:9090/'); ``` Run it like: $ node hello.js # Express For web apps, many sources recommend using the "express" module to provide basic MVC glue. $ npm install express Express does not include a template engine. Try Jade. http://jade-lang.com/ npm installs node modules at `$NODE_PATH` or `$PWD/node_modules/` by default. ``` var express = require('express'); var app = express.createServer(); app.get('/', function(req, res) { res.send('Welcome to node express.'); }); app.listen(9090); ``` # Typescript TypeScript is a JavaScript variant that adds a static type checker. ``` interface User { name: string; id: number; } const user: User = { name: "Paul", id: 0 }; function deleteUser(user: User) { // ... } function getAdminUser(): User { // ... } ``` # Links * [JavaScript Concepts to Know Before Learning Node.js (freeCodeCamp)](https://www.freecodecamp.org/news/javascript-concepts-to-know-before-learning-node-js/) * [Express/Node introduction (MDN)](https://developer.mozilla.org/en-US/docs/Learn/Server-side/Express_Nodejs/Introduction) * [Introduction to Node.js (nodejs.org)](https://nodejs.org/en/learn/getting-started/introduction-to-nodejs) * [Node.js server without a framework (MDN)](https://developer.mozilla.org/en-US/docs/Learn/Server-side/Node_server_without_framework) * [TypeScript Tooling in 5 minutes](https://www.typescriptlang.org/docs/handbook/typescript-tooling-in-5-minutes.html)