LoginSignup
6
7

More than 5 years have passed since last update.

express4系のルーティング/リクエストについて

Last updated at Posted at 2014-11-14

express4系でのリクエストバインド

どこに書けばいいか分からないので、とりあえず/routes/hoge.jsを作成してその中に書く。

index.js
var express = require('express');
var router = express.Router();
// GET users listing. 
module.exports = {
    form : function (req, res)  {                         
          res.render('form',{ title: 'Hello express'});  //ejsファイル呼び出し
    },
    check : function (req, res) {
        res.render('check',{ title: 'Hello express'});  //ejsファイル呼び出し
    }
};

 
 app.jsの中にリクエストに対するルートを書く。localhost:port/、localhost:port/form、localhost:port/checkで、hoge.jsの処理を実行する。(この場合ただの画面遷移)

app.js
{省略}

var routes = require('./routes/index');
var hoge = require('./routes/hoge');

{省略}

app.get('/',routes);
app.get('/form',hoge.form);
app.post('/check',hoge.check);

{省略}

 次は、ejsファイルにボタンとかつけて、ボタン押したら画面遷移とかしてみる。
 DBにデータ放り込んだり、出したりもしてみる。

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