5 | | Create nodejs web app |
| 5 | 1. First, create a new directory where all the files would live. In this directory create a package.json file that describes your app and its dependencies: file package.json as following |
| 6 | {{{ |
| 7 | { |
| 8 | "name": "docker_web_app", |
| 9 | "version": "1.0.0", |
| 10 | "description": "Node.js on Docker", |
| 11 | "author": "First Last <first.last@example.com>", |
| 12 | "main": "server.js", |
| 13 | "scripts": { |
| 14 | "start": "node server.js" |
| 15 | }, |
| 16 | "dependencies": { |
| 17 | "express": "^4.16.1" |
| 18 | } |
| 19 | } |
| 20 | }}} |
| 21 | With your new package.json file, run npm install. If you are using npm version 5 or later, this will generate a package-lock.json file which will be copied to your Docker image. |
| 22 | |
| 23 | 2. Then, create a server.js file that defines a web app using the Express.js framework: |
| 24 | |
| 25 | {{{ |
| 26 | 'use strict'; |
| 27 | |
| 28 | const express = require('express'); |
| 29 | |
| 30 | // Constants |
| 31 | const PORT = 8080; |
| 32 | const HOST = '0.0.0.0'; |
| 33 | |
| 34 | // App |
| 35 | const app = express(); |
| 36 | app.get('/', (req, res) => { |
| 37 | res.send('Hello World'); |
| 38 | }); |
| 39 | |
| 40 | app.listen(PORT, HOST, () => { |
| 41 | console.log(`Running on http://${HOST}:${PORT}`); |
| 42 | }); |
| 43 | }}} |
| 44 | |
| 45 | |