A few years ago I was doing some fooling around with creating my own RESTful services server.
Granted these examples were written in Java and Java is a general-purpose, class-based, object-oriented programming language. Java programs can be a bit wordy. These examples were written 2017 and there were easier ways even back then but even since then more and more frameworks have been created to make life easier.
One such solution is using NodeJS and some of the supporting modules. NodeJS is a platform built on top of Chrome’s javascript runtime. NodeJS is both the runtime environment as well as the JavaScript libraries.
It is possible to create a service that you can connect to and transfer data to with only a few lines.
var http = require('http'); http.createServer(function (req, res) { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello World\n'); }).listen(3000, "127.0.0.1"); console.log('Server running at http://127.0.0.1:3000/');
This server is super simple. Ok, ok, it is not terribly functional. Perhaps I am getting ahead of myself. First lets install software on your machine.
Installing NodeJS on Linux Mint
Installing NodeJS on a Linux machine, just like most software Linux installations, tends to be just a few lines.
- sudo apt-get install curl software-properties-common
- curl -sL https://deb.nodesource.com/setup_14.x | sudo bash –
- sudo apt-get install nodejs
I chose the long term version 14 instead of the latest release. What you choose would depend on what features you need if you are a bit more flexible (or perhaps less dependent on new features).
Your version can be verified with the following command.
node -v > v14.18.2
Beyond the javascript that your programs are developed in it is possible to download additional modules which can be used to encapsulate functionality into little package. This can be done in a few different ways but one of the most common is to use the npm, formerly called Node Package Manager, to download new packages. Installing a single package is done simply by doing an “npm install” followed by the package name.
$ npm install express npm WARN saveError ENOENT: no such file or directory, open '/home/chris/working/nodejs/simple-server/package.json' npm notice created a lockfile as package-lock.json. You should commit this file. npm WARN enoent ENOENT: no such file or directory, open '/home/chris/working/nodejs/simple-server/package.json' npm WARN simple-server No description npm WARN simple-server No repository field. npm WARN simple-server No README data npm WARN simple-server No license field. + express@4.17.1 added 50 packages from 37 contributors and audited 50 packages in 1.892s found 0 vulnerabilities
Simple Server
The smallest functional server written in NodeJS would one that listens to a specific endpoint for any of the common HTTP messages (GET,PUT,POST,DELETE). This example program will listen to port 8181 on the server.
var express = require('express'); var app = express(); app.get('/', function (req, res) { res.send('Hello World'); }) var server = app.listen(8081, function () { let port = server.address().port console.log(`Example app listening on port ${port} `) })
This can be tested by running the server on your local machine attaching to it from your web browser.
Running the server is super easy as well, simply run the NodeJS (node) with the name of the script to run.
> node server.js Server running at http://127.0.0.1:3000/
Looking at the code you would expect to see your classic Hello World to show up when your visit this endpoint.
It is important to install any modules that you will use. There is quite a bit of existing functionality but the express module needs to be installed before it can be used.