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

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

查看所有标签

猜你喜欢:

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

免费

免费

[美] 克里斯·安德森 / 蒋旭峰、冯斌、璩静 / 中信出版社 / 2009-9 / 39.00

在《免费:商业的未来 》这本书,克里斯·安德森认为,新型的“免费”并不是一种左口袋出、右口袋进的营销策略,而是一种把货物和服务的成本压低到零的新型卓越能力。在上世纪“免费”是一种强有力的推销手段,而在21世纪它已经成为一种全新的经济模式。 究竟什么是免费商业模式?根据克里斯·安德森的说法,这种新型的“免费”商业模式是一种建立在以电脑字节为基础上的经济学,而非过去建立在物理原子基础上的经济学。......一起来看看 《免费》 这本书的介绍吧!

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具