Link to Express Server

Using NPM `link` option

nick3499
Oct 28, 2020

NPM link express

A project’s express installation amounts to 400k+, since that would include various dependencies. While a Linux distro probably has the express server available in it’s repo, just as the Debian repo does. In the latter case, npm has the link option available, which allows a developer to link or symlink their existing express module installation to their project’s root directory. In that way, such dependency bloat can be avoided.

This example should work with any common Linux distro. First navigate to the project’s root directory (as with the example below), then execute the following in a Unix-like terminal emulater (e.g. Bash):

$ cd /scripts/js/hello-world/
$ npm link express

The terminal emulator should return something like the following:

/scripts/js/node_modules/express -> /home/user/.nvm/versions/node/v14.15.0/lib/node_modules/express

Note: that nvm path is not typical.

Server Application

In the Unix-like shell, execute the following to run the node REPL:

$ node

Then, enter the following instructions into the REPL:

> const express = require('express')
> const app = express()
> const port = 3000
> app.get('/', (req, res) => {
... res.send('Hello World!')
... })
> app.listen(port, () => {
... console.log(`Webserver URL: localhost:${port} or 127.0.0.1:${port}`)
... })

Which should return:

> Webserver URL: localhost:3000 or 127.0.0.1:3000

Finally, open a webbrowser to the URL localhost:3000, and Hello World! should be displayed in the upper left corner.

--

--

nick3499
nick3499

Written by nick3499

coder of JavaScript and Python

No responses yet