Express.js – app.listen vs server.listen

The second form (creating an HTTP server yourself, instead of having Express create one for you) is useful if you want to reuse the HTTP server, for example to run socket.io within the same HTTP server instance: var express = require(‘express’); var app = express(); var server = require(‘http’).createServer(app); var io = require(‘socket.io’).listen(server); … server.listen(1234); … Read more

How to download a file with Node.js (without using third-party libraries)?

You can create an HTTP GET request and pipe its response into a writable file stream: const http = require(‘http’); // or ‘https’ for https:// URLs const fs = require(‘fs’); const file = fs.createWriteStream(“file.jpg”); const request = http.get(“http://i3.ytimg.com/vi/J—aiyznGQ/mqdefault.jpg”, function(response) { response.pipe(file); // after download completed close filestream file.on(“finish”, () => { file.close(); console.log(“Download Completed”); }); … Read more

Error message “error:0308010C:digital envelope routines::unsupported”

Here are two options now – 1. Try to uninstall Node.js version 17+ and reinstall Node.js version 16+ You can re install the current LTS Node.js version from their official site. Or more specific downloads from here; You can use NVM (Node Version Manager) Linux and Mac users can use this nvm package – https://github.com/nvm-sh/nvm … Read more

How do I run a node.js app as a background service?

Copying my own answer from How do I run a Node.js application as its own process? 2015 answer: nearly every Linux distro comes with systemd, which means forever, monit, PM2, etc are no longer necessary – your OS already handles these tasks. Make a myapp.service file (replacing ‘myapp’ with your app’s name, obviously): [Unit] Description=My … Read more

npm install vs. update – what’s the difference?

The difference between npm install and npm update handling of package versions specified in package.json: { “name”: “my-project”, “version”: “1.0”, // install update “dependencies”: { // —————— “already-installed-versionless-module”: “*”, // ignores “1.0” -> “1.1” “already-installed-semver-module”: “^1.4.3” // ignores “1.4.3” -> “1.5.2” “already-installed-versioned-module”: “3.4.1” // ignores ignores “not-yet-installed-versionless-module”: “*”, // installs installs “not-yet-installed-semver-module”: “^4.2.1” // installs … Read more