Store messages in database
This commit is contained in:
parent
fbce70df08
commit
07424b2849
1
.gitignore
vendored
1
.gitignore
vendored
@ -1 +1,2 @@
|
|||||||
node_modules
|
node_modules
|
||||||
|
data.db
|
||||||
|
36
index.js
36
index.js
@ -1,5 +1,13 @@
|
|||||||
const express = require('express')
|
import express from 'express'
|
||||||
var exphbs = require('express-handlebars')
|
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()
|
const app = express()
|
||||||
|
|
||||||
@ -8,19 +16,16 @@ app.set('view engine', 'handlebars');
|
|||||||
|
|
||||||
app.use(express.urlencoded())
|
app.use(express.urlencoded())
|
||||||
|
|
||||||
const messages = [
|
app.get('/', async (req, res) => {
|
||||||
'hello world',
|
const db = await dbPromise;
|
||||||
'is anybody out there',
|
const messages = await db.all('SELECT * FROM Message;')
|
||||||
'who\'s there?'
|
|
||||||
]
|
|
||||||
|
|
||||||
app.get('/', (req, res) => {
|
|
||||||
res.render('home', { messages })
|
res.render('home', { messages })
|
||||||
})
|
})
|
||||||
|
|
||||||
app.post('/message', (req, res) => {
|
app.post('/message', async (req, res) => {
|
||||||
|
const db = await dbPromise
|
||||||
const messageText = req.body.messageText
|
const messageText = req.body.messageText
|
||||||
messages.push(messageText)
|
await db.run('INSERT INTO Message (text) VALUES (?);', messageText)
|
||||||
res.redirect('/')
|
res.redirect('/')
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -28,6 +33,11 @@ app.get('/time', (req, res) => {
|
|||||||
res.send('the current time is ' + (new Date()).toLocaleTimeString())
|
res.send('the current time is ' + (new Date()).toLocaleTimeString())
|
||||||
})
|
})
|
||||||
|
|
||||||
app.listen(8000, () => {
|
const setup = async () => {
|
||||||
|
const db = await dbPromise
|
||||||
|
await db.migrate()
|
||||||
|
app.listen(8000, () => {
|
||||||
console.log('listening on localhost: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": "",
|
"description": "",
|
||||||
"main": "index.js",
|
"main": "index.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
|
"start": "node -r esm index.js",
|
||||||
"test": "echo \"Error: no test specified\" && exit 1"
|
"test": "echo \"Error: no test specified\" && exit 1"
|
||||||
},
|
},
|
||||||
"author": "",
|
"author": "",
|
||||||
"license": "ISC",
|
"license": "ISC",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"esm": "^3.2.25",
|
||||||
"express": "^4.17.1",
|
"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>
|
<ul>
|
||||||
<li>this is always there</li>
|
<li>this is always there</li>
|
||||||
{{#each messages}}
|
{{#each messages}}
|
||||||
<li style="font-style:italic">{{this}}</li>
|
<li style="font-style:italic">{{id}}: {{text}}</li>
|
||||||
{{/each}}
|
{{/each}}
|
||||||
</ul>
|
</ul>
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user