Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Fedora - Using Fedora to quickly implement REST API with JavaScript

#1
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



https://www.sickgaming.net/blog/2020/04/...avascript/
Reply



Possibly Related Threads…
Thread Author Replies Views Last Post
  Fedora - Contribute to Fedora 39 Upgrade, Virtualization, and Cloud Test Day xSicKxBot 0 3,784 09-30-2023, 03:56 AM
Last Post: xSicKxBot
  Fedora - Share your game achievements with Gamerzilla xSicKxBot 0 2,121 09-27-2023, 09:59 AM
Last Post: xSicKxBot
  Fedora - Using Cockpit to graphically manage systems, without installing Cockpit on xSicKxBot 0 1,987 09-26-2023, 06:41 AM
Last Post: xSicKxBot
  Fedora - Announcing Fedora Linux 39 Beta xSicKxBot 0 2,012 09-20-2023, 09:48 AM
Last Post: xSicKxBot
  Fedora - Contribute at Passkey Auth, Fedora CoreOS and IoT Test Week xSicKxBot 0 2,032 09-19-2023, 12:23 PM
Last Post: xSicKxBot
  Fedora - Quick Fedora shirt update and sale of last stock with the old logo xSicKxBot 0 2,052 09-16-2023, 12:28 PM
Last Post: xSicKxBot
  Fedora - Contribute at the Fedora Linux Test Week for Kernel 6.5 and Toolbx Test Day xSicKxBot 0 2,235 09-11-2023, 02:47 PM
Last Post: xSicKxBot
  Fedora - Fedora Linux Flatpak cool apps to try for September xSicKxBot 0 1,976 09-10-2023, 04:59 PM
Last Post: xSicKxBot
  Fedora - Contribute at the Test Week for the Anaconda WebUI Installer for Fedora Wor xSicKxBot 0 1,998 09-09-2023, 11:54 PM
Last Post: xSicKxBot
  Fedora - Docs workshop: Virtually writing together xSicKxBot 0 2,006 09-09-2023, 05:08 AM
Last Post: xSicKxBot

Forum Jump:


Users browsing this thread:
1 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016