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

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

查看所有标签

猜你喜欢:

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

OpenCV3编程入门

OpenCV3编程入门

毛星云 / 电子工业出版社 / 2015-2 / 79

OpenCV在计算机视觉领域扮演着重要的角色。作为一个基于开源发行的跨平台计算机视觉库,OpenCV实现了图像处理和计算机视觉方面的很多通用算法。本书以当前最新版本的OpenCV最常用最核心的组件模块为索引,深入浅出地介绍了OpenCV2和OpenCV3中的强大功能、性能,以及新特性。书本配套的OpenCV2和OpenCV3双版本的示例代码包中,含有总计两百多个详细注释的程序源代码与思路说明。读者......一起来看看 《OpenCV3编程入门》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

HTML 编码/解码
HTML 编码/解码

HTML 编码/解码

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

HSV CMYK互换工具