docker-comsepose
- ファイルを作成する
version: '3' services: express: image: node:14.15.1-alpine container_name: express ports: - '3000:3000' volumes: - ./express:/opt/express/ tty: true
docker-build
docker-compose build
初期化
npm init
コマンドを実行する
# npm init { "name": "express", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "author": "", "license": "ISC" }
dockerにログインして作業する
- docker ログイン
docker exec -it express ash
- expressをインストールする
# npm install express --save
- 下記のエラーが発生する
npm ERR! code ENOSELF npm ERR! Refusing to install package with name "express" under a package npm ERR! also called "express". Did you name your project the same npm ERR! as the dependency you're installing? npm ERR! npm ERR! For more information, see: npm ERR! <https://docs.npmjs.com/cli/install#limitations-of-npms-install-algorithm> npm ERR! A complete log of this run can be found in: npm ERR! /root/.npm/_logs/2021-03-25T23_04_42_464Z-debug.log
- package.jsonのnameをexpressから違う名前に変更する
{ "name": "demo", (※※ 省略 ※※) }
- 再度インストールするとインストールすることができる
# npm install express --save
- pegをインストールする
npm install pug
- views/index.pugファイルを作成する
html head title= title body h1= message
- app.jpファイルを作成する
const express = require('express') const app = express() const port = 3000 app.get('/', (req, res) => { res.render('index', { title: 'Hey', message: 'Hello there!' }) }) app.set('view engine', 'pug') app.listen(port, () => { console.log(`Example app listening at http://localhost:${port}`) })
サーバー起動
node app.js