Skip to content

Nuxt.js + VercelでBlade Online Formatterを作った

Posted on:2020-12-15 at 09:23 AM

Nuxt.js + VercelでBlade Online Formatterを作った

やったこと

ブラウザ上で動作するBlade Formatterを作った

url: https://online-blade-formatter.vercel.app/

source code: GitHub

Motivation

Vercel

Vercel所感

Cold Start対策

const https = require('https');

exports.handler = async(event) => {
  let dataString = '';

  const request1 = new Promise((resolve, reject) => {
    const req = https.get("https://online-blade-formatter.vercel.app/", function(res) {
      res.on('data', chunk => {
        console.log('ok')
      });
      res.on('end', () => {
        resolve({
          statusCode: 200,
          body: 'ok'
        });
      })
    });

    req.on('error', (e) => {
      reject({
        statusCode: 500,
        body: 'Something went wrong'
      });
    });
  });

  const request2 = new Promise((resolve, reject) => {
    const data = JSON.stringify({
      data: 'foo',
    })

    const options = {
      hostname: 'online-blade-formatter.vercel.app',
      port: 443,
      path: '/v1/format',
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
        'Content-Length': data.length,
      },
    }

    const req = https.request(options, (res) => {
      res.on('data', (d) => {
        process.stdout.write(d);
      })
      res.on('end', () => {
        resolve({
          statusCode: 200,
          body: 'ok'
        });
      })
    })

    req.on('error', (e) => {
      reject({
        statusCode: 500,
        body: 'Something went wrong'
      });
    });

    req.write(data)
    req.end();
  });

  const response = await Promise.all([request1, request2]).then(res => {
    return {
      statusCode: 200,
      body: 'ok'
    }
  }).catch(res => {
    return {
      statusCode: 500,
      body: 'error'
    }
  });

  return response;
};

苦労したところ

まとめ