Using Fedora to quickly implement REST API with JavaScript

Fedora Workstation uses GNOME Shell by default and this one was mainly written in JavaScript. JavaScript is famous as a language of front-end development but this time we’ll show its usage for back-end.

We’ll implement a new API using the following technologies: JavaScript, Express and Fedora Workstation. A web browser is being used to call the service (eg. Firefox from the default Fedora WS distro).

Installing of necessary packages

Check: What’s already installed?

$ npm -v
$ node -v

You may already have both the necessary packages installed and can skip the next step. If not, install nodejs:

$ sudo dnf install nodejs

A new simple service (low-code style)

Let‘s navigate to our working directory (work) and create a new directory for our new sample back-end app.

$ cd work
$ mkdir newApp
$ cd newApp
$ npx express-generator

The above command generates an application skeleton for us.

$ npm i

The above command installs dependencies. Please mind the security warnings – never use this one for production.

Crack open the routes/users.js

Modify line #6 to:

res.send(data);

Insert this code block below var router:

let data = {
 '1':'Ann',
 '2': 'Bruno',
 '3': 'Celine'
 }

Save the modified file.

We modified a route and added a new variable data. This one could be declared as a const as we didn‘t modify it anywhere. The result:

Running the service on your local Fedora workstation machine

$ npm start

Note: The application entry point is bin/www. You may want to change the port number there.

Calling our new service

Let‘s launch our Firefox browser and type-in:

http://localhost:3000/users

Output

It‘s also possible to leverage the Developer tools. Hit F12 and in the Network tab, select the related GET request and look at the side bar response tab to check the data.

Conclusion

Now we have got a service and and an unnecessary index accessible through localhost:3000. To get quickly rid of this:

  1. Remove the views directory
  2. Remove the public directory
  3. Remove the routes/index.js file
  4. Inside the app.js file, modify the line 37 to:
    res.status(err.status || 500).end();
  5. Remove the next line res.render(‘error’)

Then restart the service:

$ npm start

For Developers For System Administrators

8 Comments

  1. J-Money

    Very nice simple guide! Thanks for sharing.

  2. Ednilton Curt Rauh

    Hello, I have been a user of the fedora system since version 16. I have been following the evolution of O.S. I use it for programming and development of Java, Spring, React and Angular systems. I would like to know if Fedora has a Cloud for testing for its users.

    • Howdy, I deployed a Red Hat server for a SMB in the past but was mainly using different environments for software development and office workflows. I‘d need to know more about your use case for a more accurate answer (maybe an RedHat insider can tell you what exactly you can have). The example I wrote can be deployed to a cloud too. You can choose OpenStack, Vagrant or AWS for example. See https://alt.fedoraproject.org/cloud/ for more details. I expect more options as the IBM’s acquisition expands possibilities in the near future.

  3. Stephen Sadowski

    Shouldn’t this just be titled ‘using nodejs to implement a REST api with javascript’? There’s nothing Fedora-centric here other than using dnf to install node.

  4. Stable and ok article for web developers. Thank you for it.

  5. I thing you forgot to copy all code, it’s only picture.

    I never coded in javascript before, I tested this script and failed because some typo.

    • Jason

      The code is created by running ‘npx express-generator’. If you followed the entire article, you’d know that the only purpose of that screenshot was to demonstrate that ‘let’ can be replaced with ‘const’ … but it works either way.

Comments are Closed

The opinions expressed on this website are those of each author, not of the author's employer or of Red Hat. Fedora Magazine aspires to publish all content under a Creative Commons license but may not be able to do so in all cases. You are responsible for ensuring that you have the necessary permission to reuse any work on this site. The Fedora logo is a trademark of Red Hat, Inc. Terms and Conditions