Giter Site home page Giter Site logo

kevinschaich / react-full-stack-starter Goto Github PK

View Code? Open in Web Editor NEW
25.0 4.0 8.0 22 KB

🎈Full-stack React boilerplate using `create-react-app`, Babel, Node.js, and express

License: MIT License

HTML 40.85% JavaScript 59.15%
react reactjs full-stack fullstack fullstack-boilerplate fullstack-javascript react-app create-react-app express expressjs nodejs node node-js babel es6 mongodb mongoose mongo database-connector database

react-full-stack-starter's Introduction

React Full-Stack Starter Kit

Full-stack React boilerplate using create-react-app, Babel, Node.js, and express. Plays nicely with DB connectors such as MongoDB, Mongoose, MySQL and many others.

Fully-updated for ES6 syntax.

Loosely Based on Fullstack React's demo, just leaned-out. Check out their blog post for details on how the proxy setup allows a concurrent client/server side.

Installation/Usage

Run the following in your terminal:

git clone https://github.com/kevinschaich/react-full-stack-starter
cd react-full-stack-starter
npm install

cd client
npm install

cd ..
npm start

Visit http://localhost:3000 in your browser to see the server running!

Note for Windows Users: If you encounter errors during installation, I recommend giving CMDer a try.

Example DB Connection with MongoDB

Install MongoDB

Make sure you have MongoDB installed. If you don't have any databases set up, you can run this command to populate a few rows (be sure to change db-name and collection-name):

mongo db-name --eval 'db.collection-name.insert({"name": "John Doe"}, {"name": "Jane Doe"})'

Run the following in the root directory of the repository:

npm install --save mongodb

Configure MongoDB

In the top of server.js, add the following lines to import Mongo and set the database URI. Be sure to replace database-name-here with the name of your database in Mongo.

const MongoClient = require('mongodb').MongoClient;
const url = 'mongodb://localhost:27017/database-name-here';

Retrieve objects from DB

Now, near the bottom of server.js, update the app.get('/api'...) route to retrieve data from your DB. Be sure to replace collection-name-here with the name of your collection in Mongo.

app.get('/api', (req, res) => {
  MongoClient.connect(url).then(db => {
    const cursor = db.collection('collection-name-here')
      .find({})
      .limit(5)
      .toArray()
    .then((data) => {
      res.json(data);
    });
  }).catch(err => {
    console.log(err);
  });
});

Your server should be pulling items from the database when it receives a call to /api. You can test this by visiting http://localhost:3001/api and see if the response is displayed properly.

Update Client Code

Back on the client side, in client/src/App.js, you need to update your render method to match the format of objects in MongoDB. For example, if your stored objects in Mongo look like the following:

[
  {"name": "Person1", "age": 38},
  {"name": "Person2", "age": 27},
]

You could change the mapping to populate the name field of each item on the page like so:

  const items = this.state.items.map(
    (item, i) => (<h1 key={i}>{item.name}</h1>)
  );

Run the server using npm start -- you should see items from your DB being populated on the page!

Deploying to Heroku

Install the Heroku CLI and set up your account if you haven't already.
Follow the instructions here.

Run the following in your terminal

# cd into client folder and run the build script
cd client
npm run build
cd ..

# commit the changes to git
git add .
git commit -m "build for deployment"

# create the heroku app and deploy
heroku create
git push heroku master

Contributing/Pull Requests

Please feel free to submit issues/pull requests! I welcome any corrections or suggestions that could make the repository better for others to use and build off of as well.

License

MIT © Kevin Schaich

react-full-stack-starter's People

Contributors

kevinschaich avatar saltynoodles avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.