Created
October 9, 2018 18:49
-
-
Save onefriendaday/7d2e1aaa72d6a0295e96f6f998bbbd85 to your computer and use it in GitHub Desktop.
Simple contact form with Storyblok and Nodejs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| Instructions: | |
| 1. Install the dependencies with "npm init" and "npm install storyblok-js-client http" | |
| 1. Create a content type with a "message" field type | |
| 2. Exchange oauthToken (from the my account section of Storyblok) and spaceId | |
| 3. Start the server with "node index.js" and execute following curl request | |
| curl -X POST \ | |
| http://127.0.0.1:3020 \ | |
| -H 'Content-Type: application/json' \ | |
| -d '{ | |
| "message": "test" | |
| }' | |
| */ | |
| const http = require('http') | |
| const StoryblokClient = require('storyblok-js-client') | |
| const port = 3020 | |
| const host = '127.0.0.1' | |
| const oauthToken = 'YOUR_TOKEN' | |
| const spaceId = 'YOUR_SPACE_ID' | |
| const client = new StoryblokClient({ | |
| oauthToken: oauthToken | |
| }) | |
| const server = http.createServer((req, res) => { | |
| if (req.method == 'POST') { | |
| let text_body = '' | |
| req.on('data', (data) => { | |
| text_body += data; | |
| }); | |
| req.on('end', () => { | |
| let requestBody = JSON.parse(text_body) | |
| console.log('Storyblok Parameters: ' + text_body) | |
| let timestamp = Math.floor(Date.now() / 1000) | |
| let payload = { | |
| publish: '1', | |
| story: { | |
| name: 'Contact entry - ' + timestamp, | |
| content: { | |
| message: requestBody.message | |
| } | |
| } | |
| } | |
| client.post(`spaces/${spaceId}/stories`, payload) | |
| .then((response) => { | |
| console.log('story #' + response.data.story.id + ' created') | |
| }) | |
| .catch((res) => { | |
| console.error(res) | |
| }) | |
| }) | |
| // end with 204 - no content | |
| res.writeHead(204, { 'Content-Type': 'application/json' }) | |
| res.end() | |
| } else { | |
| let html = '<html><head><title>Contact form</title></head><body>Your contact form server is ready</body></html>' | |
| res.writeHead(200, {'Content-Type': 'text/html'}) | |
| res.end(html) | |
| } | |
| }) | |
| server.listen(port, host) | |
| console.log('Listening at http://' + host + ':' + port) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment