0
Webアプリルーティング初心者はこちら。私はNode/Expressをベースにした比較的シンプルなアプリケーションを持っています。投稿内/投稿後のエクスプレスレンダリング
アプリの主なインデックスは、ユーザー名とIDのリストです。
localhost:242
すべてのユーザーIDは、その特定のユーザーに関する追加のメタデータを入力するフォームを持つページへのリンクです。
localhost:242/user/1398
すべて正常に動作しています。ユーザーに関するメタデータを入力してフォームを送信すると、POSTルートが実行され、元のページにリダイレクトされます。リダイレクトを使用する代わりに、同じページを再レンダリングできるようにしたいので、変更されたばかりのことを示す確認メッセージを渡すことができます。
ここに私のコードの簡略版があります。
// Module imports
var express = require('express');
var validator = require('express-validator');
var router = express.Router();
router.get('/', function(req, res, next) {
db.dataTalk(queryUsers, null, config.connection, function(err, result) {
var listUsers = result;
res.render('index', {
// Index with list of users
title: 'Page Title',
listUsers: listUsers
});
});
});
// GET /user/:id
router.get('/user/:id', function(req, res, next) {
db.dataTalk(queryUserDeets, [req.params.id], config.connection, function(err, result) {
// Details for a single user
var userDetails = result;
res.render('user', {
title: req.params.id,
userDetails: userDetails
});
});
});
// POST /user-update
router.post('/user-update', function(req, res) {
// Here goes a lot of logic to validate the form contents, and update the appropriate databases
// Redirect back to the user page, which should display the updated metadata
res.redirect('/user/' + req.body.userInitID);
});
module.exports = router;
リダイレクトを 'res.render()'に置き換えないだけの理由はありますか? – jfriend00