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

  • 相关阅读:
    窗体吸附 Timer + 判断Location (简单实用)
    C# FTP 应用程序
    C# 加密方法汇总
    LINQ 标准的查询操作符 合计操作符 Count()、Sum()、Min()、Max()、Average()和Aggregate()
    委托中的协变和逆变(C# 编程指南)
    深入探讨C#序列化和反序列化
    grep 命令详解
    Oracle 数据库的启动和关闭的方式!
    linux 下的光盘拷贝
    C3P0连接池配置
  • 原文地址:https://www.cnblogs.com/Answer1215/p/14330653.html
Copyright © 2011-2022 走看看