
In this tutorial series, you’ll see how to get started with creating a blogging app using React. Throughout the course of this tutorial series, you’ll be focusing on how to use React for developing the application user interface. You’ll be using Node.js for the server side of the application.
In this tutorial, you’ll see how to implement the user interface and back end for user registration and user sign-in.
Getting Started
Create a project directory called ReactNodeApp. Navigate to the project directory and initiate the node project.
npm init
Fill in the required details and you should have the package.json file created. Here is how it looks:
{
"name": "react-node-app",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo "Error: no test specified" && exit 1"
},
"author": "roy",
"license": "MIT"
}
You’ll be using the express framework for serving your application. Install express using the following command:
npm install express --save
Using the express framework, let’s create our application listening on a port address. Inside the project directory, create a file called app.js. Require the express module inside the app.js and create an app. Set the static path of the application where it can find the static files. Here is how it looks:
var express = require("express");
var path = require("path");
var app = express();
app.use(express.static(path.join(__dirname,"/html")));
Assign a port number for the application to listen on a port. Add the following code to create a server:
app.listen(7777,function(){
console.log("Started listening on port", 7777);
})
Inside the project directory, create a folder called html. Inside the html folder, create a file called index.html. Add the following code to index.html:
Hello World
Save the above changes and start the server using the following command:
node app.js
Point your browser to http://localhost:7777/index.html and you should be able to see the index.html page.
Creating the Sign-In View
You’ll be using bootstrap to create the user interface. Download and include bootstrap in the index.html page.
Add the required React libraries in the index.html page.
You’ll be creating the view using JSX. If you are not familiar with JSX, I would recommend reading an introductory tutorial on React and JSX.
To transform JSX code to JavaScript, you’ll need babel, a JavaScript compiler. Include babel in the index.html page.
Create a file called main.jsx inside the html folder. This file will contain the React UI components.
Let’s create a new React component called Signin inside the main.jsx file.
class Signin extends React.Component {
}
Add a render method inside the Signin component which will display the UI for our Signin component.
class Signin extends React.Component {
render() {
return (
)
}
}
In the above code, it’s all HTML with just one difference. The class attribute has been modified to className when used in JSX.
The Signin component, when displayed, will display the HTML code inside the render method.
Add a container div in the index.html page where you’ll render the Signin component.
Render the Signin component inside the .container div in the index.html.
ReactDOM.render( <
Signin / > ,
document.getElementById('app')
);
Save the above changes and restart the node server. Point your browser to http://localhost:7777/index.html and you should be able to view the sign-in screen.

Implementing User Sign-In
To implement the sign-in process, you need to handle the input text onChange event and keep the text box values in a state variable. When the user clicks the button, you’ll make use of the state variables to read the email address and password text box values. So, let’s add the onChange event to the text boxes:
Define the onChange events in the Signin component:
handleEmailChange(e){
this.setState({email:e.target.value})
}
handlePasswordChange(e){
this.setState({password:e.target.value})
}
Bind the above defined events and the state variables in the component constructor method:
constructor(props) {
super(props);
this.handleEmailChange = this.handleEmailChange.bind(this);
this.handlePasswordChange = this.handlePasswordChange.bind(this);
this.state = {
email:'',
password:''
};
}
Define an onClick method which you’ll invoke on button click.
signIn(){
alert('Email address is ' + this.state.email + ' Password is ' + this.state.password);
}
Add the OnClick event to the SignIn button.
Save the above changes and restart the node server. Point your browser to http://localhost:7777/index.html. Enter the email address and password and click the Sign In button, and you should be able to see the email and password pop up.
Posting Data From React to the Node Service
Once you have the data on the client side, you need to post that data to the Node.js server method to validate the user sign-in. For posting the data, you’ll make use of another script called axios. Axios is a promise-based HTTP client for the browser and Node.js. Include axios in the index.html page.
Inside the signin method in the main.jsx file, add the following line of code to make a post request.
axios.post('/signin', {
email: this.state.email,
password: this.state.password
})
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
The code makes a post request to the /signin method with the parameters shown. Once the request is successful, the promise is resolved in the then callback. On error, the response is logged in the catch callback.
Let’s create a signin method on the Node.js side to validate the user sign-in process. In the app.js file, create a method called signin.
app.post('/signin', function (req, res) {
})
You’ll be making use of the body-parser module to parse the request posted from the React client side. Install the body-parser module in the project.
npm install body-parser --save
Require the body-parser module in the app.js file.
var bodyParser = require("body-parser");
Add the following line of code to enable JSON parsing.
app.use(bodyParser.json());
Inside the signin method, you can parse the request as shown:
var user_name=req.body.email; var password=req.body.password;
Modify the signin method as shown to validate the user sign-in.
app.post('/signin', function (req, res) {
var user_name=req.body.email;
var password=req.body.password;
if(user_name=='admin' && password=='admin'){
res.send('success');
}
else{
res.send('Failure');
}
})
For the time being, the user credentials have been hard-coded. You can replace this with the appropriate service as per your preference.
Once the parameters have been parsed, they are validated against the expected credentials. If true, a success message is passed, else a failure message is returned.
Save the above changes and restart the Node.js server. Enter a valid username and password and click the sign-in method. Based on the credentials, it will return a success or failure message, which will be displayed in the browser console.
Creating the User Registration View
The process of creating the user registration view is quite similar to how you implemented the user sign-in module. Let’s start by creating the Signup component in the main.jsx file.
class Signup extends React.Component{
render() {
return (
)
}
}
Since sign-up and sign-in are two different components, you need to link the two components. For the purpose of routing, you’ll be making use of react-router. If you are new to routing in React, I would recommend reading the React routing tutorial.
Include react-router in the index.html page.
Define the required react-router variables to create links in the main.jsx file.
var Router = window.ReactRouter.Router; var Route = window.ReactRouter.Route; var hashHistory = window.ReactRouter.hashHistory; var Link = window.ReactRouter.Link;
Define the different application routes and the default route as shown below:
ReactDOM.render(
,
document.getElementById('app'));
Include a link to the sign-in component in the sign-up component and vice versa. Here is the Signin component’s render method with the sign-up link:
render() {
return (
{'Signup'}
)
}
Save the above changes and restart the Node.js server. Point your browser to http://localhost:7777/index.html and you should be able to see the sign-in screen with the sign-up link. Click on the sign-up link and you should be navigated to the sign-up screen.

Implementing user sign-up is similar to how you implemented user sign-in. I’ll leave the user sign-up implementation as an exercise. I’ll post the user sign-up implementation in the next part of this tutorial series.
Wrapping It Up
In this part of the tutorial series, you created and implemented the Sign In screen. You also saw how to use react-router to implement routing in React. In the next part of this tutorial, you’ll see how to implement the Sign Up portion and the Add Post page.
Source code from this tutorial is available on GitHub.
Do let us know your thoughts and suggestions in the comments below.












