我正在创建一个Node.JS网站,该网站需要创建POST端点才能为我的GET路由检索有效负载json数据,以便页面以保留的名和姓氏刷新。我该怎么做?我已将GET路由与POST端点json数据中所需的必要键/值包括在内。
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',
// This woould be the json data I need from the payload
firstName: json.payload.data.Source_FirstName,
lastname: json.payload.data.Source_LastName,
// end of data needed
date: moment().format('ll'),
time: moment().format('LT'),
currentHour: welcomeTime.dayTime()
});
});
// POST Route
app.post('/hotel', function(req, res){
res.redirect('/', {
});
});
// Start Server
app.listen(5000, function(){
console.log('Port 5000 LIVE.');
});
我已经包含了我正在使用的GET Route。
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">
</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">
© 2018 NodePark, All rights reserved.
</div>
我的目标是从POST端点检索数据,以将我的GET路由加载到我的网站上。