Archive for the ‘expressjs’ Category

nodejs: expressjs: use express generator to create web applications

July 20, 2017

This post discusses using express generator to create web applications.

software version

bash-3.2$ node -v
v8.1.4
bash-3.2$ npm -v
5.0.3
bash-3.2$ express --version
4.15.0

what is express generator
A command line tool to automatically generate web application skeleton.

install npm generator

bash-3.2$ npm install express-generator -g
/usr/local/bin/express -> /usr/local/lib/node_modules/express-generator/bin/express-cli.js
+ express-generator@4.15.0
added 7 packages in 1.979s
bash-3.2$ express -h

  Usage: express [options] [dir]

  Options:

    -h, --help           output usage information
        --version        output the version number
    -e, --ejs            add ejs engine support
        --pug            add pug engine support
        --hbs            add handlebars engine support
    -H, --hogan          add hogan.js engine support
    -v, --view   add view  support (dust|ejs|hbs|hjs|jade|pug|twig|vash) (defaults to jade)
    -c, --css    add stylesheet  support (less|stylus|compass|sass) (defaults to plain css)
        --git            add .gitignore
    -f, --force          force on non-empty directory

use express generator to create web applications

bash-3.2$ express --view=pug myapp

   create : myapp
   create : myapp/package.json
   create : myapp/app.js
   create : myapp/public
   create : myapp/routes
   create : myapp/routes/index.js
   create : myapp/routes/users.js
   create : myapp/views
   create : myapp/views/index.pug
   create : myapp/views/layout.pug
   create : myapp/views/error.pug
   create : myapp/bin
   create : myapp/bin/www
   create : myapp/public/javascripts
   create : myapp/public/images
   create : myapp/public/stylesheets
   create : myapp/public/stylesheets/style.css

   install dependencies:
     $ cd myapp && npm install

   run the app:
     $ DEBUG=myapp:* npm start

run the app: a web server

bash-3.2$ cd myapp && npm install
bash-3.2$ DEBUG=myapp:* npm start

> myapp@0.0.0 start /Users/chengyihe/workspace/node/myapp
> node ./bin/www

  myapp:server Listening on port 3000 +0ms

use browser to see the web page

what does npm start do
By default, it calls scripts.start property. In this case, scripts.start = “node ./bin/www”. Therefore, executing “npm start” is equivalent to executing “node ./bin/www”.

{
  "name": "myapp",
  "version": "0.0.0",
  "private": true,
  "scripts": {
    "start": "node ./bin/www"
  },
  "dependencies": {
    "body-parser": "~1.17.1",
    "cookie-parser": "~1.4.3",
    "debug": "~2.6.3",
    "express": "~4.15.2",
    "morgan": "~1.8.1",
    "pug": "~2.0.0-beta11",
    "serve-favicon": "~2.4.2"
  }
}

conclusion
The post shows how to use express generator tool to create a simple web server.

nodejs: expressjs: a simple web server

July 20, 2017

This post discusses how to create a simple server by express.js.

software version

$ node -v
v8.1.4
$ npm -v
5.0.3

what is express.js
A web frameworks based on nodejs

use npm to create an application dependent on express module
Use npm init to create an application, myapp. Set app.js as entry point of myapp. Then call npm install express to make express module a dependency of myapp.

$ mkdir myapp
$ cd myapp
$ npm init
This utility will walk you through creating a package.json file.
It only covers the most common items, and tries to guess sensible defaults.

See `npm help json` for definitive documentation on these fields
and exactly what they do.

Use `npm install <pkg>` afterwards to install a package and
save it as a dependency in the package.json file.

Press ^C at any time to quit.
package name: (myapp)
version: (1.0.0)
description:
entry point: (index.js) app.js
test command:
git repository:
keywords:
author:
license: (ISC)
About to write to /Users/chengyihe/workspace/node/myapp/package.json:

{
"name": "myapp",
"version": "1.0.0",
"description": "",
"main": "app.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC"
}

Is this ok? (yes)
$ npm install express
npm notice created a lockfile as package-lock.json. You should commit this file.
npm WARN myapp@1.0.0 No description
npm WARN myapp@1.0.0 No repository field.

+ express@4.15.3

create a web server with express module
Below is a server to handle get, post and put request to / path.

const express = require('express')
const app = express()

app.get('/', function (req, res) {
res.send('Response for a GET request\n')
})

app.post('/', function (req, res) {
res.send('Response for a POST request\n')
})

app.put('/', function (req, res) {
res.send('Response for a PUT request\n')
})

app.listen(3000, function () {
console.log('Example app listening on port 3000!')
})

Run the server

$ node app.js
Example app listening on port 3000!

client gets different response while using different htp request

$ curl -i -X GET http://127.0.0.1:3000/
HTTP/1.1 200 OK
X-Powered-By: Express
Content-Type: text/html; charset=utf-8
Content-Length: 27
ETag: W/"1b-pN9sXU7r0A2IQd0ldy0JSeuB4Kc"
Date: Thu, 20 Jul 2017 01:56:39 GMT
Connection: keep-alive

Response for a GET request
$ curl -i -X POST http://127.0.0.1:3000/
HTTP/1.1 200 OK
X-Powered-By: Express
Content-Type: text/html; charset=utf-8
Content-Length: 28
ETag: W/"1c-ElHCAfDja6bDIja0jD2CpSBPkUg"
Date: Thu, 20 Jul 2017 02:02:59 GMT
Connection: keep-alive

Response for a POST request
$ curl -i -X PUT http://127.0.0.1:3000/
HTTP/1.1 200 OK
X-Powered-By: Express
Content-Type: text/html; charset=utf-8
Content-Length: 27
ETag: W/"1b-urf98BOc+00dqwrJXZgwCKqWe8w"
Date: Thu, 20 Jul 2017 02:03:27 GMT
Connection: keep-alive

Response for a PUT request

conclusion
The post show a simple web server created by express.js.


%d bloggers like this: