Store messages in database
This commit is contained in:
		
							
								
								
									
										1
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										1
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							| @@ -1 +1,2 @@ | ||||
| node_modules | ||||
| data.db | ||||
|   | ||||
							
								
								
									
										38
									
								
								index.js
									
									
									
									
									
								
							
							
						
						
									
										38
									
								
								index.js
									
									
									
									
									
								
							| @@ -1,5 +1,13 @@ | ||||
| const express = require('express') | ||||
| var exphbs  = require('express-handlebars') | ||||
| import express from 'express' | ||||
| import exphbs from 'express-handlebars' | ||||
|  | ||||
| import sqlite3 from 'sqlite3' | ||||
| import { open } from 'sqlite' | ||||
|  | ||||
| const dbPromise = open({ | ||||
|   filename: 'data.db', | ||||
|   driver: sqlite3.Database | ||||
| }) | ||||
|  | ||||
| const app = express() | ||||
|  | ||||
| @@ -8,19 +16,16 @@ app.set('view engine', 'handlebars'); | ||||
|  | ||||
| app.use(express.urlencoded()) | ||||
|  | ||||
| const messages = [ | ||||
|   'hello world', | ||||
|   'is anybody out there', | ||||
|   'who\'s there?' | ||||
| ] | ||||
|  | ||||
| app.get('/', (req, res) => { | ||||
| app.get('/', async (req, res) => { | ||||
|   const db = await dbPromise; | ||||
|   const messages = await db.all('SELECT * FROM Message;') | ||||
|   res.render('home', { messages }) | ||||
| }) | ||||
|  | ||||
| app.post('/message', (req, res) => { | ||||
| app.post('/message', async (req, res) => { | ||||
|   const db = await dbPromise | ||||
|   const messageText = req.body.messageText | ||||
|   messages.push(messageText) | ||||
|   await db.run('INSERT INTO Message (text) VALUES (?);', messageText) | ||||
|   res.redirect('/') | ||||
| }) | ||||
|  | ||||
| @@ -28,6 +33,11 @@ app.get('/time', (req, res) => { | ||||
|   res.send('the current time is ' + (new Date()).toLocaleTimeString()) | ||||
| }) | ||||
|  | ||||
| app.listen(8000, () => { | ||||
|   console.log('listening on localhost:8000') | ||||
| }) | ||||
| const setup = async () => { | ||||
|   const db = await dbPromise | ||||
|   await db.migrate() | ||||
|   app.listen(8000, () => { | ||||
|     console.log('listening on localhost:8000') | ||||
|   }) | ||||
| } | ||||
| setup() | ||||
|   | ||||
							
								
								
									
										10
									
								
								migrations/001-initial-schema.sql
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										10
									
								
								migrations/001-initial-schema.sql
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,10 @@ | ||||
| -- Up | ||||
|  | ||||
| CREATE TABLE Message ( | ||||
|   id INTEGER PRIMARY KEY, | ||||
|   text STRING | ||||
| ); | ||||
|  | ||||
| -- Down | ||||
|  | ||||
| DROP TABLE Message; | ||||
							
								
								
									
										823
									
								
								package-lock.json
									
									
									
										generated
									
									
									
								
							
							
						
						
									
										823
									
								
								package-lock.json
									
									
									
										generated
									
									
									
								
							
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							| @@ -4,12 +4,16 @@ | ||||
|   "description": "", | ||||
|   "main": "index.js", | ||||
|   "scripts": { | ||||
|     "start": "node -r esm index.js", | ||||
|     "test": "echo \"Error: no test specified\" && exit 1" | ||||
|   }, | ||||
|   "author": "", | ||||
|   "license": "ISC", | ||||
|   "dependencies": { | ||||
|     "esm": "^3.2.25", | ||||
|     "express": "^4.17.1", | ||||
|     "express-handlebars": "^5.3.2" | ||||
|     "express-handlebars": "^5.3.2", | ||||
|     "sqlite": "^4.0.23", | ||||
|     "sqlite3": "^5.0.2" | ||||
|   } | ||||
| } | ||||
|   | ||||
| @@ -3,7 +3,7 @@ | ||||
| <ul> | ||||
|   <li>this is always there</li> | ||||
|   {{#each messages}} | ||||
|   <li style="font-style:italic">{{this}}</li> | ||||
|   <li style="font-style:italic">{{id}}: {{text}}</li> | ||||
|   {{/each}} | ||||
| </ul> | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user