nodejs Node 環境構築(express)

本稿について

本稿はサイト運営者が学んだことなどを記したメモの内容です。
全てが正確な情報とは限りません。ご注意ください。また修正するべき点は適時修正していきます
最新のnodeをインストールしておく

npm init
[koibuchitetsuya] ~/work/friendly-node% 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 init` 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: (hogehoge)
version: (1.0.0)
description:
entry point: (index.js)
test command:
git repository: https://github.com/xxxx/xxxxx
keywords:
author:
license: (ISC)
About to write to ….../package.json:

.node-versionを作成する(avn-nvmを入れておくとversionを自然に設定してくれる
$ vim .node-version
v12.18.4


expressを入れた
$ npm install express
$ npm install -g express-generator

expressのプロジェクトの作成方法
$ express “アプリケーション名(ディレクトリ名になる)”

expressの起動方法(bin/wwwが起動される)
$ npm start

デーモンでの起動方法
$ forever start bin/www
warn:    --minUptime not set. Defaulting to: 1000ms
warn:    --spinSleepTime not set. Your script will exit if it does not stay up for at least 1000ms
info:    Forever processing file: bin/www

再起動
$ forever restart bin/www

停止
$ forever list
info:    Forever processes running
data:        uid  command       script  forever pid   id logfile                        uptime
data:    [0] FYFW /usr/bin/node bin/www 64268   69713    /home/ubuntu/.forever/FYFW.log STOPPED

$ forever forever stop FYFW




gaeで動かすためにserver.jsを作った

server.js
const express = require('express');
const app = express();


app.get('/', (req, res) => {
  res.send('Hello from App Engine!');
});


// Listen to the App Engine-specified port, or 8080 otherwise
const PORT = process.env.PORT || 8080;
app.listen(PORT, () => {
  console.log(`Server listening on port ${PORT}...`);
});

gaeで動かすために以下のyamlを作成しておく
app.yaml
runtime: nodejs12

yamlのリファレンス


[参考]

関連ページ

node HTTP通信

#nodejs

nvm

#nodejs
Back