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

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

查看所有标签

猜你喜欢:

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

Beginning XML with DOM and Ajax

Beginning XML with DOM and Ajax

Sas Jacobs / Apress / 2006-06-05 / USD 39.99

Don't waste time on 1,000-page tomes full of syntax; this book is all you need to get ahead in XML development. Renowned web developer Sas Jacobs presents an essential guide to XML. Beginning XML with......一起来看看 《Beginning XML with DOM and Ajax》 这本书的介绍吧!

随机密码生成器
随机密码生成器

多种字符组合密码

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器

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

HEX CMYK 互转工具