POST路线到END路线

时间:2018-09-05 14:25:59

标签: node.js rest express post handlebars.js

我正在构建Node.JS网站,需要将当前的GET Route更改为POST路由。我如何成功进行转换。我当前的错误是“无法获取/”,那么我需要使POST路由到达GET路由。

const express = require('express');
const path = require('path');
const hbs = require('express-handlebars');

// Moment.js
var moment = require('moment');

// Init App
const app = express();

// Json File
const json = require("./data.json");

// Javascript Files
const welcomeTime = require('./public/javascript/welcomeTime');

// Load View Engine
app.engine('hbs', hbs({extname: 'hbs', defaultLayout: 'layout', 
layoutsDir: __dirname + '/views/layouts/'}));
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'hbs');
app.use(express.static(__dirname+'/public'));

// GET Route
// app.get('/', function(req, res){
//     res.render('index', {
//         title: 'NodePark',
//         hotel: 'NodePark',
//         firstName: json.payload.data.Source_FirstName,
//         lastname: json.payload.data.Source_LastName,
//         date: moment().format('ll'),
//         time: moment().format('LT'),
//         currentHour: welcomeTime.dayTime()
//     });
// });

// POST Route
app.post('/', function(req, res){
    res.render('index', {
        title: 'NodePark',
        hotel: 'NodePark',
        firstName: json.payload.data.Source_FirstName,
        lastname: json.payload.data.Source_LastName,
        date: moment().format('ll'),
        time: moment().format('LT'),
        currentHour: welcomeTime.dayTime()
    });
});

// Start Server
app.listen(5000, function(){
    console.log('Port 5000 open for business.');
});

我包括了正在工作的GET路由和POST路由的尝试。我不明白为什么它不起作用。

layout.hbs在下面

<!doctype HTML>
<html lang="en">
<head>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta charset="UTF-8">
    <title>{{ title }}</title>
    <link href="https://fonts.googleapis.com/css?family=Fanwood+Text:400i" rel="stylesheet">        
    <link href="https://fonts.googleapis.com/css?family=Lora" rel="stylesheet">
    <link rel="stylesheet" href="/stylesheets/style.css">
    <style>li span { position: relative; left: -8px; }</style>
</head>
<body>
    {{{ body }}}
</body>
</html>

index.hbs在下面

    <div id="body-container">
    <div id="header">
        <div id="logo-content">
            <div id="logo"></div>
        </div>
        <div id="welcome">
            <p>Good {{ currentHour }} {{ firstName }} {{ lastname }}. We hope your stay is comfortable & relaxing.</p>
        </div>
        </div>
        {{!-- <div id="time-side">
            <div id="time-content">
                <p> {{ date }} <br> {{ time }} </p>
            </div>
        </div> --}}
    </div> 
    {{!-- end of header --}}

    <div id="background">
        <img src="images/villaView.jpg" alt="villaView">
    </div>

    <div id="footer">
        <p> <br> &copy; 2018 NodePark </p>
    </div>

我的目标是创建一个从POST路由到GET路由的端点。

1 个答案:

答案 0 :(得分:2)

在您的帖子处理程序中,调用res.redirect("<new-url>")将用户重定向到<new-url>

// POST Route
app.post('/', function(req, res){
    // store the incoming data somewhere, e.g. in a cookie-based session
    res.redirect('/');
});

// GET Route
app.get('/', function(req, res){
    // retrieve the posted data from somewhere, e.g. from a cookie-based session
    res.render('index', { ... });
});