继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

Node.js 编程返回动态 HTML

phodal
关注TA
已关注
手记 60
粉丝 113
获赞 1769

原标题:Serverless 应用开发指南: Node.js 编程返回动态 HTML

在我们进行 Serverless + SPA 应用开发之前,先看看官方的相应 DEMO。

serverless install -u https://github.com/serverless/examples/tree/master/aws-node-serve-dynamic-html-via-http-endpoint -n node-serve-html

然后执行部署

serverless deploy

serverless.yml 文件,如下:

# Serving HTML through API Gateway for AWS Lambdaservice: node-serve-htmlframeworkVersion: ">=1.1.0 <2.0.0"provider:
  name: aws
  runtime: nodejs4.3functions:
  landingPage:
    handler: handler.landingPage
    events:
      - http:
          method: get
          path: landing-page

对应的,我们的 handler.js 文件:

'use strict';module.exports.landingPage = (event, context, callback) => {  let dynamicHtml = '<p>Hey Unknown!</p>';  // check for GET params and use if available  if (event.queryStringParameters && event.queryStringParameters.name) {
    dynamicHtml = `<p>Hey ${event.queryStringParameters.name}!</p>`;
  }  const html = `
  <html>    <style>
      h1 { color: #73757d; }
    </style>
    <body>
      <h1>Landing Page</h1>
      ${dynamicHtml}    </body>
  </html>`;  const response = {    statusCode: 200,    headers: {      'Content-Type': 'text/html',
    },    body: html,
  };  // callback is sending HTML back
  callback(null, response);
};

上面的代码所做的就是,当我们对 landing-page 发出请求的时候,便执行上面的 landingPage 代码。然后返回对应的 HTML body、statusCode、headers。

相应的部署日志如下:

..............................
Serverless: Stack update finished...
Service Information
service: node-serve-html
stage: dev
region: us-east-1stack: node-serve-html-dev
api keys:
  None
endpoints:  GET - https://uocym5fe3m.execute-api.us-east-1.amazonaws.com/dev/landing-page
functions:
  landingPage: node-serve-html-dev-landingPage

然后我们访问:https://uocym5fe3m.execute-api.us-east-1.amazonaws.com/dev/landing-page,就会返回对应的 HTML,即:

Landing Page

Hey phodal!


打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP