Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Fedora - Using Fedora to implement REST API in JavaScript: part 2

#1
Using Fedora to implement REST API in JavaScript: part 2

<div><p>In <a href="https://fedoramagazine.org/using-fedora-to-quickly-implement-rest-api-with-javascript/">part 1</a> previously, you saw how to quickly create a simple API service using Fedora Workstation, Express, and JavaScript. This article shows you the simplicity of how to create a new API. This part shows you how to:</p>
<ul>
<li>Install a DB server</li>
<li>Build a new route</li>
<li>Connect a new datasource</li>
<li>Use Fedora terminal to send and receive data</li>
</ul>
<p> <span id="more-31061"></span> </p>
<h2>Generating an app</h2>
<p>Please refer to the <a href="https://fedoramagazine.org/using-fedora-to-quickly-implement-rest-api-with-javascript/">previous article</a> for more details. But to make things simple, change to your work directory and generate an app skeleton.</p>
<pre class="wp-block-code"> <div class="codecolorer-container text default" style="overflow:auto;border:1px solid #9F9F9F;width:435px"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace">$ cd our-work-directory<br />
$ npx express-generator –no-view –git /myApp<br />
$ cd myApp<br />
$ npm i</div></div> </pre>
<h2>Installing a database server</h2>
<p>In this part, we’ll install MariaDB database. MariaDB is the Fedora default database.</p>
<pre class="wp-block-preformatted">$ dnf module list mariadb | sort -u ## lists the streams available
$ sudo dnf module install mariadb:10.3 ##10.4 is the latest</pre>
<p><em>Note: the default profile is mariadb/server</em>.</p>
<p>For those who need to spin up a Docker container a ready made container with Fedora 31 is available.</p>
<pre class="wp-block-preformatted">$ docker pull registry.fedoraproject.org/f31/mariadb<br />$ docker run -d --name mariadb_database -e MYSQL_USER=user -e MYSQL_PASSWORD=pass -e MYSQL_DATABASE=db -p 3306:3306 registry.fedoraproject.org/f31/mariadb</pre>
<p>Now start the MariaDB service.</p>
<pre class="wp-block-preformatted">$ sudo systemctl start mariadb</pre>
<p>If you’d like the service to start at boot, you can also enable it in systemd:</p>
<pre class="wp-block-preformatted">$ sudo systemctl enable mariadb ## start at boot</pre>
<p>Next, setup the database as needed:</p>
<pre class="wp-block-preformatted">$ mysql -u root -p ## root password is blank
MariaDB&gt; CREATE DATABASE users;
MariaDB&gt; create user dbuser identified by ‘123456‘;
MariaDB&gt; grant select, insert, update, create, drop on users.* to dbuser;
MariaDB&gt; show grants for dbuser;
MariaDB&gt; \q</pre>
<p>A database connector is needed to use the database with Node.js.</p>
<pre class="wp-block-preformatted">$ npm install mariadb ## installs MariaDB Node.js connector</pre>
<p>We’ll leverage Sequelize in this sample API. Sequelize is a promise-based Node.js ORM (Object Relational Mapper) for Postgres, MySQL, MariaDB, SQLite and Microsoft SQL Server.</p>
<pre class="wp-block-preformatted">$ npm install sequelize ## installs Sequelize</pre>
<h2>Connecting a new datasource</h2>
<p>Now, create a new <em>db</em> folder and create a new file <em>sequelize.js</em> there:</p>
<pre class="wp-block-preformatted">const Sequelize = require('sequelize'), sequelize = new Sequelize(process.env.db_name || 'users', process.env.db_user || 'dbuser', process.env.db_pass || '123456', { host: 'localhost', dialect: 'mariadb', ssl: true
}) module.exports = sequelize</pre>
<p><em>Note: For the sake of completeness I‘m including a link to the related Github repo: <a href="https://github.com/vaclav18/express-api-mariadb">https://github.com/vaclav18/express-api-mariadb</a></em></p>
<p>Let‘s create a new file <em>models/user.js</em>. A nice feature of a Sequelize model is that it helps us to create the necessary tables and colums automatically. The code snippet responsible for doing this is seen below:</p>
<pre class="wp-block-preformatted">sequelize.sync({
force: false
})</pre>
<p>Note: never switch to true with a production database – it would<em> drop your tables at app start</em>!</p>
<p>We will refer to the earlier created sequelize.js this way:</p>
<pre class="wp-block-preformatted">const sequelize = require('../db/sequelize')</pre>
<h2>Building new routes</h2>
<p>Next, you’ll create a new file <em>routes/user.js</em>. You already have <em>routes/users.js</em> from the previous article. You can copy and paste the code in and proceed with editing it.</p>
<p>You’ll also need a reference to the previously created model.</p>
<pre class="wp-block-preformatted">const User = require('../models/user')</pre>
<p>Change the route path to <em>/users</em> and also create a new <strong>post</strong> method route.</p>
<p>Mind the async – await keywords there. An interaction with a database will take some time and this one will do the trick. Yes, an async function returns a promise and this one makes promises easy to use.</p>
<p><em>Note: This code is not production ready, since it would also need to include an authentication feature.</em></p>
<p>We‘ll make the new route working this way:</p>
<pre class="wp-block-preformatted">const userRouter = require('./routes/user')
app.use(userRouter)</pre>
<p>Let‘s also remove the existing <em>usersRouter</em>. The <em>routes/users.js</em> can be deleted too.</p>
<pre class="wp-block-preformatted">$ npm start</pre>
<p>With the above command, you can launch your new app.</p>
<h2>Using the terminal to send and retrieve data</h2>
<p>Let’s create a new database record through the post method:</p>
<pre class="wp-block-preformatted">$ curl -d 'name=Adam' http://localhost:3000/users</pre>
<p>To retrieve the data created through the API, do an HTTP GET request:</p>
<pre class="wp-block-preformatted">$ curl http://localhost:3000/users</pre>
<p>The console output of the curl command is a JSON array containing data of all the records in the <em>Users</em> table.</p>
<p><em>Note: This is not really the usual end result — an application consumes the API finally. The API will usually also have endpoints to update and remove data.</em></p>
<h2>More automation</h2>
<p>Let‘s assume we might want to create an API serving many tables. It‘s possible and very handy to automatically generate models for Sequelize from our database. Sequelize-auto will do the heavy lifting for us. The resulting files (<em>models.js</em>) would be placed and imported within the <em>/models</em> directory.</p>
<pre class="wp-block-preformatted">$ npm install sequelize-auto</pre>
<p>A node.js connector is needed to use this one and we have it already installed for MariaDB.</p>
<h2>Conclusion</h2>
<p>It‘s possible to develop and run an API using Fedora, Fedora default MariaDB, JavaScript and efficiently develop a solution like with a noSQL database. For those used to working with MongoDB or a similar noSQL database, Fedora and MariaDB are important open-source enablers.</p>
<hr class="wp-block-separator" />
<p><em>Photo by <a href="https://unsplash.com/@m47h4r?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Mazhar Zandsalimi</a> on <a href="https://unsplash.com/s/photos/javascript?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a>.</em></p>
</div>


https://www.sickgaming.net/blog/2020/05/...pt-part-2/
Reply



Forum Jump:


Users browsing this thread:
2 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016