LoginSignup
0
0

More than 3 years have passed since last update.

Node.js(五):Express 実装

Posted at

今回からはNodeでExpressを勉強しましょう、Expressは人気なのJSバックエンドモジュールと言うことです。先ずはインストールしてみよう。

実装の手順

Node.js立ち上げ

新たなフォルダを作って、エントリポイントとしてのapp.js追加して、vscode中のターミナルでポロジェクトにnode.jsをインストする。

xxx.xxx:my-first-express$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 json` 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: (my-server) 
version: (1.0.0) 
description: 
entry point: (app.js) 
test command: 
git repository: 
keywords: 
author: 
license: (ISC) 
license: (ISC) 
About to write to /express/my-server/package.json:

{
  "name": "my-server",
  "version": "1.0.0",
  "description": "",
  "main": "app.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC"
}


Is this OK? (yes) yes

そして、package.jsonは作成しました。

pachage.json
{
  "name": "my-server",
  "version": "1.0.0",
  "description": "",
  "main": "app.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC"
}

Expressインストール

ターミナルでnpmでexpressをインストールします


xxx.xxx:my-first-express$ npm install express
npm notice created a lockfile as package-lock.json. You should commit this file.
npm WARN my-server@1.0.0 No description
npm WARN my-server@1.0.0 No repository field.

+ express@4.17.1
added 50 packages from 37 contributors and audited 126 packages in 2.902s
found 0 vulnerabilities

そして、package.json中で確認しましょう。

package.json
{
  "name": "my-server",
  "version": "1.0.0",
  "description": "",
  "main": "app.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "express": "^4.17.1"
  }
}

"dependencies"の意味は開発者使うパッケージ、ちなみに^4.17.1の"^"はマイナバージョンまでを使われる。

Experss導入

app.js
 const express =require("express"); //express moduleをリクワイアする
 const app = express();//expressオプジェットを呼ぶ

//ポットとコールベック関数の内容を設定する
app.listen(3000,function(){
    console.log("Success to run Server!!") 
})

最後はターミナルでテストしよう

xxx.xxx:my-first-express$ node app.js
Success to run Server!!

以上です。

0
0
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
0
0