LoginSignup
2
5

More than 5 years have passed since last update.

node.js + Express 最低限のメモ

Posted at

Expressを利用して簡単な処理(API返すとか)を実行。

前提

  • Mac環境
  • Node.jsは既に入っている。

Expressのインストール

cd
mkdir express
cd express
npm install express

実装

作業フォルダにapp.jsを作成する。
とりあえずHelloWorldとAPIの基本。

app.js
var express = require('express');
var app = express();

//HelloWorld
app.get('/', function(req,res,next){
    res.send("Hello World");
});

//API
app.get('/api/:memberId', function(req,res,next){
    var memberId = req.params.memberId;
    var data = {"status":"OK","memberId":memberId}
    res.json(data);
});

//Listen
app.listen(3000, function(){
    console.log("Start Express on port 3000.");
});

実行と動作確認

node app.js

http://localhost:3000http://localhost:3000/api/12345 とかにアクセス。

2
5
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
2
5