Using Fedora to quickly implement REST API with JavaScript

栏目: IT技术 · 发布时间: 4年前

内容简介: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 it‘s usage for back-end.We‘ll implement a new API using the following technolo

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 it‘s 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 we already have?

$ npm -v
$ node -v

Maybe we already have both of them installed and can skip the next 2 install steps.

Setup dnf repo

$ sudo dnf install -y gcc-c++ make
$ curl -sL https://rpm.nodesource.com/setup_12.x | sudo -E bash -

Install Node.js

$ 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

## This one generates an application skeleton for us

$ npm i

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

Using Fedora to quickly implement REST API with JavaScript

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

Using Fedora to quickly implement REST API with JavaScript
Output

It‘s also possible to leverage the Developer tools (F12 – 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/.

How to get quickly rid of this one?

1) remove ‚views‘ directory

2) remove ‚public‘ directory

3) remove routes/index.js

4) inside app.js file:

modify the line 37 to: res.status(err.status || 500).end();

remove the next line res.render(‘error’)

Restart the service

$ npm start

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Python网络数据采集

Python网络数据采集

米切尔 (Ryan Mitchell) / 陶俊杰、陈小莉 / 人民邮电出版社 / 2016-3-1 / CNY 59.00

本书采用简洁强大的Python语言,介绍了网络数据采集,并为采集新式网络中的各种数据类型提供了全面的指导。第一部分重点介绍网络数据采集的基本原理:如何用Python从网络服务器请求信息,如何对服务器的响应进行基本处理,以及如何以自动化手段与网站进行交互。第二部分介绍如何用网络爬虫测试网站,自动化处理,以及如何通过更多的方式接入网络。一起来看看 《Python网络数据采集》 这本书的介绍吧!

SHA 加密
SHA 加密

SHA 加密工具

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具