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

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

查看所有标签

猜你喜欢:

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

数字民主的迷思

数字民主的迷思

[美] 马修·辛德曼 / 唐杰 / 中国政法大学出版社 / 2015-12-25 / CNY 39.00

马修·辛德曼著的《数字民主的迷思》主要讨论互联网对美国政治的影响,聚焦的是“民主化”这一课题。针对公众关于网络民主的美好想象与过分狂热,它通过对在线竞选、链接结构、流量模式、搜索引擎使用、博客与博主、内容生产的“规模经济”等主题的深入处理,借助大量数据图表与分析,勾勒出互联网政治的种种局限性。尤其表明,网络政治信息仍然为一小群精英与机构所创造和过滤,在网络的每一个层次和领域都仍然遵循着“赢家通吃”......一起来看看 《数字民主的迷思》 这本书的介绍吧!

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具

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

HSV CMYK互换工具