猿问

为什么我的变量没有在node.js中更新?

我已经用node.js构建了一个Web应用程序,以便通过POST请求从SNS主题接收消息并将它们记录到控制台,然后将它们显示在网页上,如果我将消息发布到主题中,则该消息会打印在控制台中,但是什么也没有我的网页上有更新吗?


这是我的代码 app.js


const express = require("express");

const request = require('request')

const bodyParser = require('body-parser')

const app = express();

app.use(express.static('public'));

app.set("view engine", "ejs")



app.get('/', function (req, res) {

  let subject = "Your Subject Will Appear Here";

  let message = "Your Message Will Appear Here";

  res.render("index", {subject:subject, message:message});

})


app.use(bodyParser.urlencoded({ extended: false }))

app.use(bodyParser.json())


app.post('/', (req, res) => {

  let body = ''

  req.on('data', (chunk) => {

    body += chunk.toString()

  })  

  req.on('end', () => {

    let payload = JSON.parse(body)

    if (payload.Type === 'Notification') {

      console.log(payload.Message);

      let subject = payload.Subject;

      let message = payload.Message;

      res.render("index", {subject:subject, message:message});

    }

  });

});


app.listen(80, process.env.IP, function(request, response){

    console.log("## SERVER STARTED ##");

});


这是我的代码 index.js


<!DOCTYPE html>

<html>

  <head>

    <meta charset="utf-8">

    <title>title</title>

    <link rel="stylesheet" type="text/css" href="/css/style.css">

    <link href='https://fonts.googleapis.com/css?family=Open+Sans:300' rel='stylesheet' type='text/css'>

  </head>

  <body>

    <div class="container">

      <fieldset>

        <% if(subject !== null){ %>

            <p>SNS Subject: <%= subject %></p>

          <% } %>

        <% if(message !== null){ %>

            <p>SNS Message: <%= message %></p>

          <% } %>

      </fieldset>

    </div>

  </body>

</html>


繁星淼淼
浏览 246回答 1
1回答
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答