https://github.com/survi218/node-express
Express framework
https://github.com/survi218/node-express
body-parser express-middleware expressjs http-requests http-response nodejs nodejs-modules postman rest-api router yargs
Last synced: 7 months ago
JSON representation
Express framework
- Host: GitHub
- URL: https://github.com/survi218/node-express
- Owner: survi218
- Created: 2017-06-03T12:30:44.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2017-06-03T19:25:13.000Z (over 8 years ago)
- Last Synced: 2025-03-05T21:57:38.745Z (7 months ago)
- Topics: body-parser, express-middleware, expressjs, http-requests, http-response, nodejs, nodejs-modules, postman, rest-api, router, yargs
- Language: JavaScript
- Homepage:
- Size: 3.91 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# node-expressjs
* Express framework to implement similar functionality as implemented by the HTTP module based servers in the previous exercise. At the end of this exercise, you will be able to:
- Implement a simple web server using Express framework
- Implement a web server that serves static content
# A Simple Server using Express
- Create a folder named node-express at a convenient location on your computer and move to that folder.
- Copy the public folder from node-http to this folder.
- Create a file named server-1.js and add the following code to it:````
var express = require('express'),
http = require('http');var hostname = 'localhost';
var port = 3000;var app = express();
app.use(function (req, res, next) {
console.log(req.headers);
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end('Hello World
');});
var server = http.createServer(app);
server.listen(port, hostname, function(){
console.log(`Server running at http://${hostname}:${port}/`);
});
````Then, install the Express framework in the folder by typing the following at the prompt:
````
npm install express --save
````Start the server by typing the following at the prompt, and then interact with the server:
````
node server-1
````Serving Static Files
Create a file named server-2.js and add the following code to it:````
var express = require('express');
var morgan = require('morgan');var hostname = 'localhost';
var port = 3000;var app = express();
app.use(morgan('dev'));
app.use(express.static(__dirname + '/public'));
app.listen(port, hostname, function(){
console.log(`Server running at http://${hostname}:${port}/`);
});
````Then, install morgan by typing the following at the prompt:
````
npm install morgan --save
````Start the server and interact with it and observe the behavior.