Welcome! Let's embark on a journey into server-side programming, starting with Node.js.
Before we pound the pavement, let's briefly define two core terms of web communication: server and client. You can think of the server as a chef - it prepares (serves) data or information. The client, on the other hand, is like a restaurant guest - it requests and receives this information and renders it on the web page. Together, they carry out web interactions.
Now, back to Node.js. So, what exactly is Node.js? It's a JavaScript runtime built on Chrome's V8 JavaScript engine, which allows you to run JavaScript on your computer. In other words, it enables you to use JavaScript outside of a browser! With Node.js, you can interact with your computer's file system much as you would with other server-side languages such as PHP or Python.
Next, let's explore Express.js. It's a flexible Node.js web application framework that offers a robust set of features for web and mobile applications. Although the name might sound intimidating, rest assured that it's a user-friendly library.
Express.js enables us to manage everything, from API routes to requests and views. It's incredibly adaptable and pluggable, providing everything we need straight out of the box.
To use Express.js in your application, just require the "express
" module in a Node.js file as shown below:
JavaScript1const express = require('express');
With Node.js and Express.js at the ready, let's proceed and create a simple server. Here's a piece of code that constructs a server that responds with "Hello World!"
to all requests:
JavaScript1const express = require('express'); // importing Express.js 2const app = express(); // Instantiating Express.js app server instance 3const port = 5000; // The server will be listening on port 5000 4 5app.get('/api', (req, res) => { 6 res.send('Hello World!'); 7}); 8 9app.listen(port, () => { 10 console.log(`Example app listening at http://localhost:${port}`); 11});
This code starts by requiring the express
module to use Express.js. We then create an instance of Express by calling the express()
function, which returns our app. The app
object possesses several methods, one of which is the get()
method, which defines a route handler for HTTP GET requests. In this case, we are defining a route handler for the root path ('/'
) that responds with the string "Hello World!"
whenever it receives a GET request. Finally, we start our server on the defined port by calling the listen()
method on our app
.
When the server is started (it happens automatically when the task is opened in CodeSignal IDE), the message "Example app listening at http://localhost:5000"
should appear. This signifies that your server is operational and listening on port 5000.
Note: A port is simply the location that our server is expecting to receive requests from. Similar to how you might expect to get files from a USB port on your computer or receive audio from a headphone port.
Now that our server is up and running let's establish a connection to our React app to get a full-stack application (frontend + backend). This can be achieved by sending a GET request to our server's endpoint (http://localhost:5000/api
) using a library named Axios. Here's how to set it up:
JavaScript1import React, { useEffect, useState } from 'react'; 2import axios from 'axios'; 3 4const App = () => { 5 const [data, setData] = useState(null); 6 7 // We don't specify `localhost:5000`, as it's already configured in CodeSignal's IDE 8 useEffect(() => axios.get('/api').then(response => { 9 setData(response.data); 10 }), []); 11 12 return ( 13 <div> 14 {data ? 15 <div>{data}</div> : 16 <div>Loading...</div> 17 } 18 </div> 19 ); 20}; 21 22export default App;
This code initiates a GET request to our server and shows the response data on the page. If you incorporate this code into a React component and launch your React app, the phrase "Hello World!"
should appear on your page. That's how you connect a server to a React app!
Congratulations! You've just created your first server with Node.js and Express.js and learned how to connect this server to a React app. Today, we've dipped our toes into the captivating world of server-side development, utilizing some powerful tools.
Remember, practicing what you've learned is an excellent way to reinforce these concepts, so expect some hands-on exercises in our next session. Good luck, and keep coding!