<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Sebastian Simancas Gonzalez</title>
    <description>The latest articles on DEV Community by Sebastian Simancas Gonzalez (@sebassd).</description>
    <link>https://dev.to/sebassd</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1112244%2Ffc314ac2-a3f6-47ae-ab94-a60ffe9cf324.jpeg</url>
      <title>DEV Community: Sebastian Simancas Gonzalez</title>
      <link>https://dev.to/sebassd</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/sebassd"/>
    <language>en</language>
    <item>
      <title>Backend deployment using Express and Json databases in React Projects</title>
      <dc:creator>Sebastian Simancas Gonzalez</dc:creator>
      <pubDate>Mon, 03 Jul 2023 03:01:08 +0000</pubDate>
      <link>https://dev.to/sebassd/backend-deployment-using-express-and-json-databases-in-react-projects-4a14</link>
      <guid>https://dev.to/sebassd/backend-deployment-using-express-and-json-databases-in-react-projects-4a14</guid>
      <description>&lt;p&gt;When building React projects, having a robust backend is essential for handling data processing, authentication, and other server-side functionalities. In this blog post, we will explore how to deploy a backend using Express, a popular Node.js framework. By leveraging Express, you can seamlessly integrate server-side logic into your React projects, allowing for a smoother user experience. So let's dive into the world of backend deployment using Express in React projects!&lt;/p&gt;

&lt;h2&gt;
  
  
  Express and Node
&lt;/h2&gt;

&lt;p&gt;Before we get started, let's briefly discuss Express and Node.js. Express is a fast and minimalist web application framework for Node.js, providing a simple and flexible way to build web applications and APIs. Node.js is an open-source, cross-platform JavaScript runtime environment that allows you to execute JavaScript code outside of a browser. Together, Express and Node.js form a powerful combination for developing server-side applications.&lt;/p&gt;

&lt;h2&gt;
  
  
  Express and JSON Databases (Configuration)
&lt;/h2&gt;

&lt;p&gt;Express integrates well with various databases, including JSON-based databases. JSON databases are lightweight and ideal for small to medium-sized projects. You can store data in JSON files or use libraries like &lt;strong&gt;json-server&lt;/strong&gt; or &lt;strong&gt;lowdb&lt;/strong&gt; to handle the database operations. These databases are easy to configure and &lt;strong&gt;provide a great way to get started with backend development in React projects.&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Logic Business Rules (Controllers and Routers)
&lt;/h2&gt;

&lt;p&gt;In Express, you can structure your backend code using controllers and routers. Controllers are responsible for handling specific logic related to routes or API endpoints, such as processing requests, interacting with the database, and sending responses. Routers define the routes for your server and map them to their respective controllers. This separation of concerns allows for better code organization and maintainability.&lt;/p&gt;

&lt;h2&gt;
  
  
  Detailed guide for Backend development using Express and JSON databases
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Step 1: Set up the project&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create a new directory for your project.&lt;/li&gt;
&lt;li&gt;Navigate to the project directory in your terminal.&lt;/li&gt;
&lt;li&gt;Initialize a new Node.js project using the npm init command.&lt;/li&gt;
&lt;li&gt;Install the necessary packages, such as Express and jsonfile, using the command npm install express jsonfile.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Step 2: Create the Express server&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create a new file, e.g., index.js, in the project directory.&lt;/li&gt;
&lt;li&gt;Open the index.js file and require the required modules: Express and jsonfile.&lt;/li&gt;
&lt;li&gt;Define the port number on which your server will run.&lt;/li&gt;
&lt;li&gt;Set up the basic route handling:&lt;/li&gt;
&lt;li&gt;Enable JSON request parsing using the express.json() middleware.&lt;/li&gt;
&lt;li&gt;Define your routes.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Step 3: Create endpoints to interact with the JSON database&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Decide on the endpoints you want to create for managing your pages (e.g.,rooms and bookings).&lt;/li&gt;
&lt;li&gt;Implement routes for handling GET, POST, PUT, and DELETE requests for these endpoints.&lt;/li&gt;
&lt;li&gt;Read the database JSON file using jsonfile.readFileSync().&lt;/li&gt;
&lt;li&gt;Use the appropriate route handler to perform the desired operations on the data (e.g., add, update, delete).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Step 4: Start the server&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Save the server.js file.&lt;/li&gt;
&lt;li&gt;Run the command node server.js in your terminal to start the server.&lt;/li&gt;
&lt;li&gt;Verify that the server is running by checking the console output for a message indicating the server is listening on the specified port.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Some Code
&lt;/h2&gt;

&lt;p&gt;Let's take a look at some code examples to illustrate how Express can be used in React projects.&lt;/p&gt;

&lt;p&gt;Example 1: Setting up a basic Express server&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const express = require('express');
const app = express();

app.get('/', (req, res) =&amp;gt; {
  res.send('Hello, world!');
});

app.listen(3000, () =&amp;gt; {
  console.log('Server started on port 3000');
});

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Example 2: Using controllers and routers&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// UserController.js
exports.getUser = (req, res) =&amp;gt; {
  // Logic to fetch user data from the database
  res.json({ name: 'John Doe', age: 25 });
};

// userRoutes.js
const express = require('express');
const router = express.Router();
const userController = require('./UserController');

router.get('/user', userController.getUser);

module.exports = router;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Example of index.js used for backend in a real project
&lt;/h2&gt;

&lt;p&gt;See github links below&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const express = require('express');
const cors = require('cors');


const app = express();
const PORT = 3009;

app.use(express.json());
app.use(cors());

// Define your routes here

const RoomsRouter = require('./routers/RoomsRouter');
const BookingRouter = require('./routers/BookingRouter');

app.use('/api', RoomsRouter);
app.use('/api', BookingRouter);

app.listen(PORT, () =&amp;gt; {
  console.log(`Server is running on port ${PORT}`);
});

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Important references
&lt;/h2&gt;

&lt;p&gt;Here are some important links related to the technologies discussed in this post:&lt;/p&gt;

&lt;p&gt;React:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;React Official Website: &lt;a href="https://reactjs.org/"&gt;https://reactjs.org/&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;React Documentation: &lt;a href="https://reactjs.org/docs/"&gt;https://reactjs.org/docs/&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;React Router (for handling client-side routing): &lt;a href="https://reactrouter.com/"&gt;https://reactrouter.com/&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;React Context API (for managing global state): &lt;a href="https://reactjs.org/docs/context.html"&gt;https://reactjs.org/docs/context.html&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;React Hooks (for managing state and side effects): &lt;a href="https://reactjs.org/docs/hooks-intro.html"&gt;https://reactjs.org/docs/hooks-intro.html&lt;/a&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;npm (Node Package Manager):&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;npm Official Website: &lt;a href="https://www.npmjs.com/"&gt;https://www.npmjs.com/&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;npm Documentation: &lt;a href="https://docs.npmjs.com/"&gt;https://docs.npmjs.com/&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Search for Packages on npm: &lt;a href="https://www.npmjs.com/search"&gt;https://www.npmjs.com/search&lt;/a&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Node.js:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Node.js Official Website: &lt;a href="https://nodejs.org/"&gt;https://nodejs.org/&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Node.js Documentation: &lt;a href="https://nodejs.org/en/docs/"&gt;https://nodejs.org/en/docs/&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Node.js Package Registry (for finding Node.js packages): &lt;a href="https://www.npmjs.com/"&gt;https://www.npmjs.com/&lt;/a&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Express:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Express Official Website: &lt;a href="https://expressjs.com/"&gt;https://expressjs.com/&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Express Documentation: &lt;a href="https://expressjs.com/en/4x/api.html"&gt;https://expressjs.com/en/4x/api.html&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Express Middleware (for extending Express functionality): &lt;a href="https://expressjs.com/en/guide/using-middleware.html"&gt;https://expressjs.com/en/guide/using-middleware.html&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Express Router (for creating modular route handlers): &lt;a href="https://expressjs.com/en/guide/routing.html"&gt;https://expressjs.com/en/guide/routing.html&lt;/a&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;These links will provide you with comprehensive documentation, examples, and resources to further explore and enhance your knowledge of these technologies.&lt;/p&gt;

&lt;h2&gt;
  
  
  React Project Development using these technologies
&lt;/h2&gt;

&lt;p&gt;Frontend : &lt;a href="https://react-frontend-omega.vercel.app/"&gt;https://react-frontend-omega.vercel.app/&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Frontend Repository:
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://github.com/sebassd/react-frontend"&gt;https://github.com/sebassd/react-frontend&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Backend Repository:
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://github.com/sebassd/backend-react"&gt;https://github.com/sebassd/backend-react&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This technologie was used for a project developed in the FULL Stack Web Development career by Flatiron School&lt;/p&gt;

&lt;p&gt;Hoping this post has been useful to your project!&lt;/p&gt;

</description>
      <category>react</category>
      <category>express</category>
      <category>javascript</category>
      <category>backend</category>
    </item>
  </channel>
</rss>
