LoginSignup
3
1

More than 3 years have passed since last update.

node.jsを使ってみる

Posted at

node.jsをインストールしてみる。
参考:https://www.sejuku.net/blog/82322

1. ダウンロード

実行してみる。

% node --version
v12.18.3

2. Hello, world!

hello.js
console.log('Hello, world!')
% node hello.js
Hello, world!

と、無事実行できた。
参考:https://qiita.com/loremipsumjp/items/3d32a44fe80c9a2febbe

3. npmを使い、expressをインストールしてみる

参考:https://qiita.com/tarotaro1129/items/e02fbb911dc4af412ad0
npmとは、パッケージ管理システム。gemみたいなもの。
expressは、node.js版の簡易なWebサーバ。WEBrickみたいなもの。

% mkdir myapp
% cd myapp
% npm init
  • ここで、10回ほどリターンを押す。package.jsonが作られる。
package name: (myapp) 
version: (1.0.0) 
description: 
entry point: (index.js) 
test command: 
git repository: 
keywords: 
author: 
license: (ISC) 
About to write to /Users/eto/dev/Day17_node/myapp/package.json:
package.json
{
  "name": "myapp",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC"
}
% npm install express --save
- package-lock.jsonが生成される。
  • app.jsというファイルを作る。
app.js
const express = require('express')
const app = express()
app.get('/', (req, res) => {
  res.send('Hello World!')
})
app.listen(8000, () => console.log('Example app listening on port 8000!'))

起動してみる。

% node app.js
Example app listening on port 8000!
Hello World!

と表示されていたら、成功。

done!

3
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1