├── .gitignore ├── README.md ├── gulpfile.js ├── package.json ├── scripts └── init.mongo.js ├── src ├── App.js ├── BugAdd.js ├── BugEdit.js ├── BugFilter.js └── BugList.js ├── static └── index.html ├── test └── test.sh └── webapp.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | static/App.js 3 | static/bundle.js 4 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # React Tutorial using MERN stack 2 | Build a complete React app, step-by-step with the MERN stack 3 | 4 | ## Introduction 5 | This is a tutorial to get started with React, using the MERN stack. You will 6 | not only learn React, but also associated environment and complimentary tools 7 | such as Mongo, Express, gulp etc. 8 | 9 | In the unlikely event that you landed here first, the complete step-by-step 10 | instructions are available 11 | [here on Hashnode](https://hashnode.com/post/react-tutorial-using-mern-stack-ciiyus9m700qqge53mer0isxz). 12 | Read and follow the tutorial there. 13 | 14 | ## 1. Hello World 15 | 16 | ### 1.1 index.html as a file 17 | Create index.html as a file, use all scripts (react, react-dom, babel) from CDN. 18 | Write a Hello World element within the HTML between ` 11 | 12 | 13 | -------------------------------------------------------------------------------- /test/test.sh: -------------------------------------------------------------------------------- 1 | # API Tests 2 | 3 | # test GET on /api/bugs: returns array of bugs 4 | curl -v localhost:3000/api/bugs 5 | 6 | # Same test with a If-None-Match that matches a previous etag. This 7 | # will return a 304. 8 | curl -v --header 'If-None-Match: W/"b5-F1b7cwQ6h9O04csif+Wqkw"' localhost:3000/api/bugs 9 | 10 | # Test POST to create a new bug and return it. 11 | # Won't work if type is not set to */* in bodyParser. 12 | curl -v \ 13 | --data '{"priority":"P3","owner":"Pieta","status":"Open","title":"New bug added via api"}' \ 14 | http://localhost:3000/api/bugs 15 | 16 | curl -v \ 17 | --header 'Content-Type: application/json' \ 18 | --data '{"priority":"P3","owner":"Pieta","status":"Open","title":"New bug added via api"}' \ 19 | http://localhost:3000/api/bugs 20 | -------------------------------------------------------------------------------- /webapp.js: -------------------------------------------------------------------------------- 1 | var express = require('express'); 2 | var bodyParser = require('body-parser'); 3 | var MongoClient = require('mongodb').MongoClient; 4 | var ObjectId = require('mongodb').ObjectID; 5 | 6 | var app = express(); 7 | var db; 8 | 9 | app.use(express.static('static')); 10 | 11 | /* Get a list of filtered records */ 12 | app.get('/api/bugs', function(req, res) { 13 | console.log("Query string", req.query); 14 | var filter = {}; 15 | if (req.query.priority) 16 | filter.priority = req.query.priority; 17 | if (req.query.status) 18 | filter.status = req.query.status; 19 | 20 | db.collection("bugs").find(filter).toArray(function(err, docs) { 21 | res.json(docs); 22 | }); 23 | }); 24 | 25 | app.use(bodyParser.json()); 26 | 27 | /* Insert a record */ 28 | app.post('/api/bugs/', function(req, res) { 29 | console.log("Req body:", req.body); 30 | var newBug = req.body; 31 | db.collection("bugs").insertOne(newBug, function(err, result) { 32 | var newId = result.insertedId; 33 | db.collection("bugs").find({_id: newId}).next(function(err, doc) { 34 | res.json(doc); 35 | }); 36 | }); 37 | }); 38 | 39 | /* Get a single record */ 40 | app.get('/api/bugs/:id', function(req, res) { 41 | db.collection("bugs").findOne({_id: ObjectId(req.params.id)}, function(err, bug) { 42 | res.json(bug); 43 | }); 44 | }); 45 | 46 | /* Modify one record, given its ID */ 47 | app.put('/api/bugs/:id', function(req, res) { 48 | var bug = req.body; 49 | console.log("Modifying bug:", req.params.id, bug); 50 | var oid = ObjectId(req.params.id); 51 | db.collection("bugs").updateOne({_id: oid}, bug, function(err, result) { 52 | db.collection("bugs").find({_id: oid}).next(function(err, doc) { 53 | res.send(doc); 54 | }); 55 | }); 56 | }); 57 | 58 | MongoClient.connect('mongodb://localhost/bugsdb', function(err, dbConnection) { 59 | db = dbConnection; 60 | var server = app.listen(3000, function() { 61 | var port = server.address().port; 62 | console.log("Started server at port", port); 63 | }); 64 | }); 65 | --------------------------------------------------------------------------------