8 Smart Bike Locks for Daily Commuters

Bicycle commuting statistics say that “From 2000 to 2013, bicycle commuting rates in large BFCs increased 105% — far above the national average of 62% and more than double the rate in non-BFCs…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




How create and deploy React.js app with Node express server

This guide is created for everyone who whats to create and deploy simple full-stack React.js on client side and ExpressJS on server side app.

Ah yes, before we start. I believe it is a good idea to show you what you’ll achieve as a result.

This is how our folder structure will look at the end.

Create root folder using your terminal commands below and navigate to it.

Now, we need to create a package.json for managing our app and run scripts.

This command creates package.json file, where we will add our scripts.

Then we need to create server folder inside the root. Where we need to create index.js file. This is our single file which will run our code.

We will use Express to create our simple server. Which will run on port 3001 as a default, for now. In the future we can pass our port values from .env file.

Now, inside the “server/index.js” file we need add this code.

We’ve created a simple express server now which listens on port 3001.

app.get creates an endpoint for the route /api. React app ( client ) makes a GET request to that route, we respond (using res, which stands for response) with our JSON data. We will come to it later in this tutorial.

In your terminal navigate to the root folder and run a command.

After installing express dependencies we want to create a script which will start our web server. Inside the /package.json in the root folder add script which will start the server.

Let’s test if we can start our server! Run

it should start our server on port 3001. the output would look like

Now move on to the Client side.

Open a new tab in your terminal for creating new React app with the project folder client.

npx create-react-app will install all necessary dependencies to our React project. When client folder with React app is created, we will need to open package.json file inside the client folder.

Proxy will allow us to make an API request to our Node server without needing to provide it every time when we make an HTTP request.

Now is a time to test our React app. Navigate to client folder from root and start the app.

the result should be

Before we move

We need to remove git from our React app which was automatically initialised by create-react-app.

Now it a time to fetch data from /api endpoint. We will need to make GET request to route /api and return the data. So let’s begin, go to client/src folder and replace code in App.js to code below.

Let’s test the app. If the server is not running you will be able to see Loading or Hello from the server in server is running. To do so, in your app navigate to your client folder and run:

If nothing is breaking, we can create and test out production build folder for client.Which we will need to use in the next chapter. So, to create a build folder in client. Run

now look at you client folder it should contain your production ready React build.

Now we need to move to the server and create an to our React build project.

This is the part where magic is about to happen.

express.static function allowing as to have an access to the static file in our case it is build folder.

Inside the root package.json we will need to add build script. Which will navigate to client folder, installs all packages and created a build. All steps which were doing earlier manually.

It will ask you to press any key to log you in via browser. Finally inside your Heroku newly created project you can navigate to “Deploy” tab and follow steps.They would look like

and finally we are ready to push our git changes to remote Heroku branch

It will enable build to run, if the build is successful…..

Your full-stack app is live!

Congratulations !!!!!Hope this tutorial helped you!

Add a comment

Related posts:

Our Objective

Our Objective with Delinet token and platform is to decentralize internet opportunities by providing a workable platform for your skills. Research has shown that, one major problem associated with…

A Comprehensive Guide to Affiliate Marketing.

Affiliate marketing is a performance-based marketing strategy where a business rewards affiliates for each customer brought about by the affiliate’s own marketing efforts. The affiliate promotes the…

Weight Loss and Health Juice Craze

Ikaria Juice is a beverage that has recently gained popularity among health enthusiasts. This juice is made from a blend of fruits and vegetables and is named after the Greek island of Ikaria, which…