Quantcast
Channel: Node.jsタグが付けられた新着記事 - Qiita
Viewing all articles
Browse latest Browse all 8829

node.jsを使ってみる

$
0
0

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
constexpress=require('express')constapp=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!


Viewing all articles
Browse latest Browse all 8829

Trending Articles