MERN Quick Start Guide
Build web applications with MongoDB, Express.js, React, and Node
Eddy Wilson Iriarte Koroliova
- 302 pages
- English
- ePUB (mobile friendly)
- Available on iOS & Android
MERN Quick Start Guide
Build web applications with MongoDB, Express.js, React, and Node
Eddy Wilson Iriarte Koroliova
About This Book
Build web applications with MongoDB, ExpressJS, React, and NodeAbout This Bookā¢ Build applications with the MERN stackā¢ Work with each component of the MERN stackā¢ Become confident with MERN and ready for more!Who This Book Is ForThe book is for JavaScript developers who want to get stated with the MERN Stack.What You Will Learnā¢ Get started with the MERN stackā¢ Install Node.js and configure MongoDBā¢ Build RESTful APIs with Express.js and Mongooseā¢ Build real-time applications with Socket.IO ā¢ Manage synchronous and asynchronous data flows with Reduxā¢ Build web applications with React In DetailThe MERN stack is a collection of great toolsāMongoDB, Express.js, React, and Nodeāthat provide a strong base for a developer to build easily maintainable web applications. With each of them a JavaScript or JavaScript-based technology, having a shared programming language means it takes less time to develop web applications. This book focuses on providing key tasks that can help you get started, learn, understand, and build full-stack web applications. It walks you through the process of installing all the requirements and project setup to build client-side React web applications, managing synchronous and asynchronous data flows with Redux, and building real-time web applications with Socket.IO, RESTful APIs, and other concepts. This book gives you practical and clear hands-on experience so you can begin building a full-stack MERN web application.Quick Start Guides are focused, shorter titles that provide a faster paced introduction to a technology. They are for people who don't need all the detail at this point in their learning curve. The presentation has been streamlined to concentrate on the things you really need to know.Style and approachThis guide shows you how to use your JavaScript knowledge to build web applications that use the MERN stack in both client-side and in server-side environments.
Frequently asked questions
Information
Building a Web server with ExpressJS
- Routing in ExpressJS
- Modular route handlers
- Writing middleware functions
- Writing configurable middleware functions
- Writing router-level middleware functions
- Writing error-handler middleware functions
- Using ExpressJS' built-in middleware function to serve static assets
- Parsing the HTTP request body
- Compressing HTTP responses
- Using an HTTP request logger
- Managing and creating virtual domains
- Securing an ExpressJS web application with helmet
- Using template engines
- Debugging your ExpressJS web application
Technical requirements
https://github.com/PacktPublishing/MERN-Quick-Start-Guide/tree/master/Chapter02
https://goo.gl/xXhqWK
Introduction
Routing in ExpressJS
- Safe: An HTTP verb that performs read-only operations on the server. In other words, it does not alter the server state. For example: GET.
- Idempotent: An HTTP verb that has the same effect on the server when identical requests are made. For instance, sending a PUT request to modify a user's first name should have the same effect on the server if implemented correctly when multiple identical requests are sent. All safe methods are also idempotent. For example, the GET, PUT, and DELETE methods are idempotent.
- Cacheable: An HTTP response that can be cached. Not all methods or HTTP verbs can be cached. A response is cacheable only if the status code of the response and the method used to make the request are both cacheable. For example, the GET method is cacheable and the following status codes: 200 (Request succeeded), 204 (No content), 206 (Partial content), 301 (Moved permanently), 404 (Not found), 405 (Method not allowed), 410 (Gone or Content permanently removed from server), and 414 (URI too long).
Getting ready
{ "dependencies": { "express": "4.16.3" } }
npm install
- Request: This contains all the data and information about the client's request. For instance, ExpressJS parses the URI and makes its parameters available on request.query.
- Response: This contains data and information that will be sent to the client. The response's headers can be modified as well before sending the information to the client. The response object has several methods available for sending the status code and data to the client. For instance: response.status(200).send('Some Data!').
How to do it...
Route methods
- Create a new file named 1-basic-route.js
- Include the ExpressJS library first and initialize a new ExpressJS application:
const express = require('express') const app = express()
- Add a new route method to handle requests for the path "/". The first argument specifies the path or URL, the next argument is the route handler. Inside the route handler, let's use the response object to send a status code 200 (OK) and text to the client:
app.get('/', (request, response, nextHandler) => { response.status(200).send('Hello from ExpressJS') })
- Finally, use the listen method to accept new connections on port 1337:
app.listen( 1337, () => console.log('Web Server running on port 1337'), )
- Save the file
- Open a Terminal and run the following command:
node 1-basic-route.js
- Open a new tab on your browser and visit localhost on port 1337 in your web browser to see the results:
http://localhost:1337/
Route handlers
- Create a new file named 2-route-handlers.js
- Include the ExpressJS libr...