@@ -30,31 +30,29 @@ See [HTTP](https://nodejs.org/api/http.html) module docs.
3030
3131### Basic
3232
33-
3433Example based on [ Getting started with NodeJS for frontend developers - Part 1] ( https://www.youtube.com/watch?v=F3eiqoNTniY&t=41s ) .
3534
3635Setup HTTP server with returns a greeting as plain text.
3736
38- ` server.js `
39- ``` javascript
40- const http = require (' http' )
37+ - ` server.js `
38+ ``` javascript
39+ const http = require (' http' )
4140
42- http .createServer ((req , resp ) => {
43- resp .writeHead (200 , { ' Content-Type' : ' text/html' });
44- resp .write (` <p>Hello, world! You requested ${ req .url } </p>` );
45- resp .end ();
46- }).listen (3000 );
47- ```
41+ http .createServer ((req , resp ) => {
42+ resp .writeHead (200 , { ' Content-Type' : ' text/html' });
43+ resp .write (` <p>Hello, world! You requested ${ req .url } </p>` );
44+ resp .end ();
45+ }).listen (3000 );
46+ ```
4847
4948Start the app . Note you won' t see any output and this is a blocking call.
5049
5150Then open the browser at:
5251
53- - http://localhost:3000/
52+ - [localhost:3000/]( http://localhost:3000/)
5453
5554Note that you can use any path and you' ll get the same response.
5655
57-
5856### Parse the query string
5957
6058Based on [Node .js HTTP Module](https: // www.w3schools.com/nodejs/nodejs_http.asp) on W3 Schools.
@@ -76,7 +74,7 @@ http.createServer(function (req, res) {
7674
7775Open in the browser:
7876
79- - http://localhost:8080/?year=2017&month=July
77+ - [localhost : 8080 / ? year = 2017 & month = July]( http: // localhost:8080/?year=2017&month=July)
8078
8179Result:
8280
@@ -108,18 +106,17 @@ Note that `return` is not used in examples below - just `resp.send`.
108106
109107[Express starter](https: // expressjs.com/en/starter/hello-world.html)
110108
111- ` server.js `
109+ - ` server.js`
110+ ` ` ` javascript
111+ const express = require('express');
112112
113- ``` javascript
114- const express = require ( ' express ' ) ;
113+ const app = express();
114+ const port = 3000 ;
115115
116- const app = express ();
117- const port = 3000 ;
116+ app.get('/', (req, resp) => resp.send('Hello, world!'))
118117
119- app .get (' /' , (req , resp ) => resp .send (' Hello, world!' ))
120-
121- app .listen (port, () => console .log (` Example app listening at http://localhost:${ port} /` ))
122- ```
118+ app.listen(port, () => console.log(` Example app listening at http: // localhost:${port}/`))
119+ ` ` `
123120
124121Run as:
125122
@@ -130,7 +127,7 @@ Example app listening at http://localhost:3000/
130127
131128Open browser at:
132129
133- - http://localhost:3000/
130+ - [localhost:3000/]( http://localhost:3000/)
134131
135132Note that you'll get an error on other paths.
136133
@@ -144,18 +141,17 @@ Add the following to base example, before `.listen`.
144141
145142Note use of ` .query ` .
146143
147-
148- ``` javascript
149- app .get (' /foo' , (req , resp ) => {
150- const { name } = req .query ;
151- resp .send (` Hello, ${ name || world} !` );
152- })
153- ```
154-
144+ - ` index .js `
145+ ` ` ` javascript
146+ app .get (' /foo' , (req , resp ) => {
147+ const { name } = req .query ;
148+ resp .send (` Hello, ${ name || world} !` );
149+ })
150+ ` ` `
155151
156152Test on:
157153
158- - http:localhost:3000/foo?name=Bar
154+ - [localhost:3000/foo?name=Bar]( http:// localhost:3000/foo?name=Bar)
159155
160156
161157### Handle URL parameters
@@ -173,44 +169,46 @@ app.get('/foo/:name', (req, resp) => {
173169
174170Test on:
175171
176- - http:localhost:3000/foo/Bar
172+ - [localhost:3000/foo/Bar]( http:// localhost:3000/foo/Bar)
177173
178174
179175### Handle POST request
180176
177+ - ` index .js `
178+ ` ` ` javascript
179+ app .use (express .json ());
180+ app .use (express .urlencoded ());
181181
182- ``` javascript
183- app .use (express .json ());
184- app .use (express .urlencoded ());
185-
186- app .post (' /foo' , (req , res ) => {
187- res .status (201 )
182+ app .post (' /foo' , (req , res ) => {
183+ res .status (201 )
188184 .send ({
189- message: " Created a foo using data" ,
190- data: req .body ,
185+ message: " Created a foo using data" ,
186+ data: req .body ,
191187 });
192- });
193- ```
188+ });
189+ ` ` `
194190
195191### Handle other status codes
196192
197- ``` javascript
198- app .get (' /admin' , (_req , res ) => {
199- res .sendStatus (401 );
200- });
201- ```
193+ - ` index .js `
194+ ` ` ` javascript
195+ app .get (' /admin' , (_req , res ) => {
196+ res .sendStatus (401 );
197+ });
198+ ` ` `
202199
203200### Static files
204201
205202Serve a directory of static files.
206203
207- ``` javascript
208- const express = require (' express' );
204+ - ` index .js `
205+ ` ` ` javascript
206+ const express = require (' express' );
209207
210- const app = express ();
211- const port = 3000 ;
208+ const app = express ();
209+ const port = 3000 ;
210+ const staticDir = ' build' ;
212211
213- const p = ' build' ;
214- app .use (' /' , express .static (p));
215- app .listen (port, () => console .log (` Example app listening at http://localhost:${ port} /` ));
216- ```
212+ app .use (' /' , express .static (staticDir));
213+ app .listen (port, () => console .log (` Example app listening at http://localhost:${ port} /` ));
214+ ` ` `
0 commit comments