Receiving CloudEvents with NextJS

One file to rule them all

Published December 15, 2021

nextjs, knative, cloudevents

Need to receive cloudevents from your NextJS app? Me too! You need next-runtime.

Install

1
  npm i next-runtime cloudevents

In pages/indes.ts

And then:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
  import { handle, json } from 'next-runtime';
  import { HTTP } from 'cloudevents';

  export const getServerSideProps = handle({
      async post({ req }) {
          console.log( req.body )
          console.log( req.headers )
        
          const receivedEvent = HTTP.toEvent({headers: req.headers, body: req.body })
          console.log( receivedEvent )
          return json({ message: 'Thanks for your submission!' });
      },
      async get() {
          return json( {message:"ok"} )
      }
  }))

Took me a while to sort out, but it wasn't so hard. Thanks meijer_s!

Previously

Setting up knative eventing lets send some messages

2021-12-13

Next

Setting up digital ocean spaces to upload Gotta work on my upload scrips

2021-12-17

howto

Previously

Setting up knative eventing lets send some messages

2021-12-13

Next

Setting up digital ocean spaces to upload Gotta work on my upload scrips

2021-12-17