React react-starter project


Example

About this Project

This is simple boilerplate project. This post will guide you to set up the environment for ReactJs + Webpack + Bable.

Lets get Started

we will need node package manager for fire up express server and manage dependencies throughout the project. if you are new to node package manager, you can check here. Note : Installing node package manager is require here.

Create a folder with suitable name and navigate into it from terminal or by GUI.Then go to terminal and type npm init this will create a package.json file, Nothing scary , it will ask you few question like name of your project ,version, description, entry point, git repository, author, license etc. Here entry point is important because node will initially look for it when you run the project. At the end it will ask you to verify the information you provide. You can type yes or modify it. Well that's it , our package.json file is ready.

Express server setup run npm install express@4 --save. This is all the dependencies we needed for this project.Here save flag is important, without it package.js file will not be updated. Main task of package.json is to store list of dependencies. It will add express version 4. Your package.json will look like "dependencies": { "express": "^4.13.4", ............. },

After complete download you can see there is node_modules folder and sub folder of our dependencies. Now on the root of project create new file server.js file. Now we are setting express server. I am going to past all the code and explain it later.

var express = require('express');
// Create our app
var app = express();

app.use(express.static('public'));

app.listen(3000, function () {
  console.log('Express server is using port:3000');
});

var express = require('express'); this will gave you the access of entire express api.

var app = express(); will call express library as function. app.use(); let the add the functionality to your express application. app.use(express.static('public')); will specify the folder name that will be expose in our web server. app.listen(port, function(){}) will here our port will be 3000 and function we are calling will verify that out web server is running properly. That's it express server is set up.

Now go to our project and create a new folder public and create index.html file. index.html is the default file for you application and Express server will look for this file. The index.html is simple html file which looks like

<!DOCTYPE html>
<html>

<head>
  <meta charset="UTF-8"/>
</head>

<body>
  <h1>hello World</h1>
</body>

</html>

And go to the project path through the terminal and type node server.js. Then you will see * console.log('Express server is using port:3000');*.

Go to the browser and type http://localhost:3000 in nav bar you will see hello World.

Now go inside the public folder and create a new file app.jsx. JSX is a preprocessor step that adds XML syntax to your JavaScript.You can definitely use React without JSX but JSX makes React a lot more elegant. Here is the sample code for app.jsx

ReactDOM.render(
  <h1>Hello World!!!</h1>,
  document.getElementById('app')
);

Now go to index.html and modify the code , it should looks like this

<!DOCTYPE html>
<html>

<head>
  <meta charset="UTF-8"/>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/babel-core/5.8.23                                              /browser.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.14.7/react.js">                           </script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.14.7/react-dom.js">    </script>
</head>

<body>
  <div id="app"></div>

  <script type="text/babel" src="app.jsx"></script>
</body>

</html>

With this in place you are all done, I hope you find it simple.