File size: 1,779 Bytes
7d9f678
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
//init all the modules we need
const express = require('express');
const { Client } = require('@notionhq/client');
const cors = require('cors');
var bodyParser = require('body-parser');
var jsonParser = bodyParser.json();
const dotenv = require('dotenv');
dotenv.config();


const app = express(); //its an express app after all

app.use(cors()); //Cross-origin resource sharing (CORS) allows AJAX requests to skip the Same-origin policy and access resources from remote hosts.

//save port number and host
const port = process.env.PORT; //4000;
const host = process.env.HOST; //localhost;

//init notion client 
const notion = new Client({ auth: "secret_7ESs8DBns0qRQtkYtSyfaVBRNTerZ6wSN6MPpBNrroR" });

//POST request
//post name, number
//functionality: add to database
//localhost:4000/add

//where u want the app to live. .post means creating a new thing
app.post("/submitFormToNotion", jsonParser, async (req, res) => { //response and request
    //req is HTTP request object, get the query, params, body, etc
    //res is HTTP response object, send the response, set the status, etc
    
    const name = req.body.name;

    try{
        const blockId = '63499a6f3fcc4960a6f7c5b985e9bb06';
        const response = await notion.blocks.children.append({
            block_id: blockId,
            children: [
              {
                "paragraph": {
                  "rich_text": [
                    {
                      "text": { "content": name }
                    }
                  ]
                }
              }
            ]
            });

    console.log(response);
    console.log("success");

    }catch(error){
        console.log(error);
    }
});

    

app.listen(port,host, () => {
  console.log(`Running on http://${host}:${port}`);
});