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 - Incremental backup with Butterfly Backup xSicKxBot 0 4 Today, 01:01 AM
Last Post: xSicKxBot
  Fedora - Web of Trust, Part 2: Tutorial xSicKxBot 0 6 10-19-2020, 08:41 PM
Last Post: xSicKxBot
  Fedora - systemd-resolved: introduction to split DNS xSicKxBot 0 11 10-16-2020, 10:28 PM
Last Post: xSicKxBot
  Fedora - Web of Trust, Part 1: Concept xSicKxBot 0 14 10-14-2020, 05:14 PM
Last Post: xSicKxBot
  Fedora - Recover your files from Btrfs snapshots xSicKxBot 0 33 10-05-2020, 07:17 PM
Last Post: xSicKxBot
  Fedora - Use dnsmasq to provide DNS & DHCP services xSicKxBot 0 40 09-30-2020, 08:14 PM
Last Post: xSicKxBot
  Fedora - Announcing the release of Fedora 33 Beta xSicKxBot 0 27 09-29-2020, 09:01 PM
Last Post: xSicKxBot
  Fedora - Now available: Fedora on Lenovo laptops! xSicKxBot 0 26 09-25-2020, 07:51 PM
Last Post: xSicKxBot
  Fedora - Installing and running Vagrant using qemu-kvm xSicKxBot 0 47 09-21-2020, 06:22 PM
Last Post: xSicKxBot
  Fedora - Incremental backups with Btrfs snapshots xSicKxBot 0 46 09-15-2020, 01:20 PM
Last Post: xSicKxBot

Forum Jump:

Become a Patron!
[-]
Upcoming Events

[-]
Latest Threads
Xbox one SDK Leaked: homebrew coming soo...
Last Post: DanielChapell
Today 05:51 PM
» Replies: 1
» Views: 1763
News - Here's How The Walking Dead Is Fi...
Last Post: xSicKxBot
Today 05:50 PM
» Replies: 0
» Views: 2
(Indie Deal) PlugInDigital & Kalypso Str...
Last Post: xSicKxBot
Today 05:06 PM
» Replies: 0
» Views: 3
(Free Game Key) Costume Quest 2 & Layers...
Last Post: xSicKxBot
Today 05:05 PM
» Replies: 0
» Views: 3
News - Destiny 2 Hotfix 2.9.2.2
Last Post: xSicKxBot
Today 01:53 PM
» Replies: 0
» Views: 3
News - The Video Game History Foundation...
Last Post: xSicKxBot
Today 01:52 PM
» Replies: 0
» Views: 4
News - French charity steam Z Event rais...
Last Post: xSicKxBot
Today 01:52 PM
» Replies: 0
» Views: 4
News - Borat 2 Review Roundup: Very Nice...
Last Post: xSicKxBot
Today 11:18 AM
» Replies: 0
» Views: 5
Mobile - Win yourself a free copy of sha...
Last Post: xSicKxBot
Today 09:07 AM
» Replies: 0
» Views: 4
[Tut] Top 3 Freelance Developer Contract...
Last Post: xSicKxBot
Today 04:49 AM
» Replies: 0
» Views: 3

[-]
Twitter

[-]
Sponsored
Get the Deal of the Week at RefurBees.com

Copyright © SickGaming.net 2012-2020