zoukankan      html  css  js  c++  java
  • [Serverless] Netlify serverless Demo

    Netlify serverless

    Set up a Local Development Environment for Serverless Functions Using Netlify

    Netlify makes developing serverless functions easy with the netlify-cli (ntl for short). You'll be able to build and test functions locally as well as publish your functions from the CLI.

    npm i -g netlify-cli
    

    Testing:

    ntl -v
    

    We will install netlify-cli globally and create a netlify.toml file that will configure where the CLI should look to run functions that we define, in our case functions/. When the application is served up, Netlify runs functions under /.netlify/functions/.

    netlify.toml:

    [build]
        publish = "public"
        functions = "functions"
    
    [dev]
        autoLaunch = false
    

    functions/hello-world.js

    exports.handler = async () => {
      return {
        statusCode: 200,
        body: "Hello world!",
      };
    };
    

    RUN:

    ntl dev
    

    Visit

    localhost:8888/.netlify/functions/hello-world.

    Deploy Serverless Functions to Production on Netlify using the Netlify CLI

    ntl login      ## login
    ntl status     ## verify
    

    Netlify makes deploying to production a breeze by configuring your Netlify CLI to your Netlify account and integrating with GitHub.

    We'll log in to Netlify through the CLI and initialize a site by completing all the options that the CLI runs us through. Once this is done, Netlify will trigger new builds for your site every time you push to GitHub.

    ntl init  ## select "Create new site"/"No Command/public folder"
    ntl open  ## open the site
    

    or you can do the same thing on the netlify website.

    Circumvent CORS when Accessing a Third-Party API using Netlify Functions

    CORS limits websites from communicating with other domains without the full consent of both sites. Consuming data from a 3rd Party REST API makes it difficult since we can't properly configure the appropriate CORS headers. To solve this, we'll set up a proxy server to request the data using a Netlify function that avoids CORS altogether.

    The Third-Party API that we'll be working with will provide the following data: id, name, favoriteSong for each corgi.

    http://no-cors-api.netlify.app/api/corgis

    We will also use node-fetch a light-weight module that brings the fetch API to fetch the data into our application.

    functions/load-corgis.js

    const fetch = require("node-fetch");
    exports.handler = async () => {
      const result = await fetch(
        "http://no-cors-api.netlify.app/api/corgis"
      ).then((res) => res.json());
    
      return {
        statusCode: 200,
        headers: {
          "Content-Type": "application/json",
        },
        body: JSON.stringify(result),
      };
    };
    

    public/index.html:

    function loadCorgis() {
        const conrgis = await fetch('/.netlify/functions/load-corgis') // load from local functions, to avoid CORS problem
            .then(res => res.json());
    
        render(
        html` ${corgis.map((corgi) => html` <${Corgi} corgi=${corgi} /> `)}`,
        document.querySelector('.corgis'),
        );
    }
    

    Import and Set Environment Variables from a .env file using Netlify CLI

    Create a .env file. It is available for myself to use.

    But if I want to share with the team. We need to share this .env file.

    netlify env:import .env
    

    If successfully imported: you will see:

    .----------------------------.
    | Imported environment variables |
    |----------------------------|
    |    Key     |     Value     |
    |------------|---------------|
    | TEST_VALUE | an test world |
    '----------------------------'
    

    The .env has been uploaded to netlify and saved there. Now even you delete the .env file, it will still be available.

    Github

  • 相关阅读:
    用代码关闭 overlay弹出层
    Asp.net CheckBoxList 多选提示
    C#获取拼音简码
    Discuz!NT的上传头像功能(Silverlight)
    在项目中使用 Discuz!NT的上传头像功能
    用户控件和href,src及其css路径问题
    截取html字符长度,并保留html格式标记
    SSSE3指令集水平加法指令饱和字节乘加指令以及字节重排指令
    SSE2指令集系列之二整数运算相关指令
    SSE指令集系列之二浮点与整数转换指令
  • 原文地址:https://www.cnblogs.com/Answer1215/p/14330653.html
Copyright © 2011-2022 走看看