diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/html/admin.html | 36 | ||||
-rw-r--r-- | src/html/bread.html | 2 | ||||
-rw-r--r-- | src/html/index.html | 2 | ||||
-rw-r--r-- | src/index.js | 3 | ||||
-rw-r--r-- | src/photo/back.jpg | bin | 1181073 -> 0 bytes | |||
-rw-r--r-- | src/photo/ban1.jpg | bin | 1106159 -> 0 bytes | |||
-rw-r--r-- | src/photo/ban2.jpg | bin | 1411456 -> 0 bytes | |||
-rw-r--r-- | src/photo/cider.jpg | bin | 725680 -> 0 bytes | |||
-rw-r--r-- | src/photo/first-1.jpg | bin | 283944 -> 0 bytes | |||
-rw-r--r-- | src/photo/first-2.jpg | bin | 296466 -> 0 bytes | |||
-rw-r--r-- | src/photo/flat1.jpg | bin | 1129498 -> 0 bytes | |||
-rw-r--r-- | src/photo/flat2.jpg | bin | 905668 -> 0 bytes | |||
-rw-r--r-- | src/photo/good1.jpg | bin | 241664 -> 0 bytes | |||
-rw-r--r-- | src/photo/good2.jpg | bin | 1847478 -> 0 bytes | |||
-rw-r--r-- | src/photo/nyt1.jpg | bin | 2142167 -> 0 bytes | |||
-rw-r--r-- | src/photo/nyt2.jpg | bin | 1286502 -> 0 bytes | |||
-rw-r--r-- | src/photo/pizza1.jpg | bin | 244008 -> 0 bytes | |||
-rw-r--r-- | src/photo/pizza2.jpg | bin | 1727783 -> 0 bytes | |||
-rw-r--r-- | src/photo/pretty.jpg | bin | 251665 -> 0 bytes | |||
-rw-r--r-- | src/photo/wheat.jpg | bin | 2669180 -> 0 bytes | |||
-rw-r--r-- | src/photo/white-1.jpg | bin | 1652366 -> 0 bytes | |||
-rw-r--r-- | src/photo/white-2.jpg | bin | 1811909 -> 0 bytes | |||
-rw-r--r-- | src/server.js | 57 |
23 files changed, 91 insertions, 9 deletions
diff --git a/src/html/admin.html b/src/html/admin.html new file mode 100644 index 0000000..7c87ba6 --- /dev/null +++ b/src/html/admin.html @@ -0,0 +1,36 @@ +<!doctype html> +<html lang="en"> + +<head> + <title>Mark's Kitchen - Admin</title> + <meta charset="UTF-8"> + <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> + <link rel="stylesheet" type="text/css" href="/css/bootstrap.css"> + <link rel="stylesheet" type="text/css" href="/css/styles.css"> +</head> + +<body> + <div> + <h1>Create Post</h1> + <div class="form"> + <form action="/posts" method="post" enctype="multipart/form-data"> + <div> + <textarea rows="4" cols="50" name="description"></textarea> + </div> + <div> + Images: <input type="file" name="images" multiple> + </div> + <div> + <select name="type"> + <option value="" disabled selected>Select...</option> + <option value="bread">Bread</option> + <option value="index">Index</option> + </select> + </div> + <input type="submit"> + </form> + </div> + </div> +</body> + +</html>
\ No newline at end of file diff --git a/src/html/bread.html b/src/html/bread.html index 8004803..e6eeb47 100644 --- a/src/html/bread.html +++ b/src/html/bread.html @@ -17,7 +17,7 @@ posts: [] }, created() { - fetch(new Request('/bread/posts.json')).then(response => response.json()) + fetch(new Request('/posts/bread')).then(response => response.json()) .then(response => this.posts = response.data); } }); diff --git a/src/html/index.html b/src/html/index.html index 7a1d5d5..c7e398b 100644 --- a/src/html/index.html +++ b/src/html/index.html @@ -18,7 +18,7 @@ posts: [] }, created() { - fetch(new Request('/index/posts.json')).then(response => response.json()) + fetch(new Request('/posts/index')).then(response => response.json()) .then(response => this.posts = response.data); } }); diff --git a/src/index.js b/src/index.js index fdcef74..b47824a 100644 --- a/src/index.js +++ b/src/index.js @@ -40,8 +40,7 @@ function setUpModels(){ type: { type: Sequelize.STRING, allowNull: false, - }, - }), + },}), "pictures": database.define('pictures', { source: { type: Sequelize.TEXT, allowNull: false}, }) diff --git a/src/photo/back.jpg b/src/photo/back.jpg Binary files differdeleted file mode 100644 index 9e8511e..0000000 --- a/src/photo/back.jpg +++ /dev/null diff --git a/src/photo/ban1.jpg b/src/photo/ban1.jpg Binary files differdeleted file mode 100644 index 0330443..0000000 --- a/src/photo/ban1.jpg +++ /dev/null diff --git a/src/photo/ban2.jpg b/src/photo/ban2.jpg Binary files differdeleted file mode 100644 index f868504..0000000 --- a/src/photo/ban2.jpg +++ /dev/null diff --git a/src/photo/cider.jpg b/src/photo/cider.jpg Binary files differdeleted file mode 100644 index 176a094..0000000 --- a/src/photo/cider.jpg +++ /dev/null diff --git a/src/photo/first-1.jpg b/src/photo/first-1.jpg Binary files differdeleted file mode 100644 index 664acd7..0000000 --- a/src/photo/first-1.jpg +++ /dev/null diff --git a/src/photo/first-2.jpg b/src/photo/first-2.jpg Binary files differdeleted file mode 100644 index 11f2ab7..0000000 --- a/src/photo/first-2.jpg +++ /dev/null diff --git a/src/photo/flat1.jpg b/src/photo/flat1.jpg Binary files differdeleted file mode 100644 index ff6df1c..0000000 --- a/src/photo/flat1.jpg +++ /dev/null diff --git a/src/photo/flat2.jpg b/src/photo/flat2.jpg Binary files differdeleted file mode 100644 index d07f638..0000000 --- a/src/photo/flat2.jpg +++ /dev/null diff --git a/src/photo/good1.jpg b/src/photo/good1.jpg Binary files differdeleted file mode 100644 index 3ff6e6b..0000000 --- a/src/photo/good1.jpg +++ /dev/null diff --git a/src/photo/good2.jpg b/src/photo/good2.jpg Binary files differdeleted file mode 100644 index bec268d..0000000 --- a/src/photo/good2.jpg +++ /dev/null diff --git a/src/photo/nyt1.jpg b/src/photo/nyt1.jpg Binary files differdeleted file mode 100644 index a31e79e..0000000 --- a/src/photo/nyt1.jpg +++ /dev/null diff --git a/src/photo/nyt2.jpg b/src/photo/nyt2.jpg Binary files differdeleted file mode 100644 index 5c955da..0000000 --- a/src/photo/nyt2.jpg +++ /dev/null diff --git a/src/photo/pizza1.jpg b/src/photo/pizza1.jpg Binary files differdeleted file mode 100644 index 051d5d0..0000000 --- a/src/photo/pizza1.jpg +++ /dev/null diff --git a/src/photo/pizza2.jpg b/src/photo/pizza2.jpg Binary files differdeleted file mode 100644 index c5e9021..0000000 --- a/src/photo/pizza2.jpg +++ /dev/null diff --git a/src/photo/pretty.jpg b/src/photo/pretty.jpg Binary files differdeleted file mode 100644 index 39afb35..0000000 --- a/src/photo/pretty.jpg +++ /dev/null diff --git a/src/photo/wheat.jpg b/src/photo/wheat.jpg Binary files differdeleted file mode 100644 index b039eb6..0000000 --- a/src/photo/wheat.jpg +++ /dev/null diff --git a/src/photo/white-1.jpg b/src/photo/white-1.jpg Binary files differdeleted file mode 100644 index a58d50b..0000000 --- a/src/photo/white-1.jpg +++ /dev/null diff --git a/src/photo/white-2.jpg b/src/photo/white-2.jpg Binary files differdeleted file mode 100644 index 87b8343..0000000 --- a/src/photo/white-2.jpg +++ /dev/null diff --git a/src/server.js b/src/server.js index 4a2a581..4cf76d1 100644 --- a/src/server.js +++ b/src/server.js @@ -1,9 +1,35 @@ const express = require('express'); +const bodyParser = require('body-parser'); const request = require('request'); +const multer = require('multer'); +var storage = multer.diskStorage({ + destination: function (req, file, cb) { + cb(null, 'src/uploads/') + }, + filename: function (req, file, cb) { + console.log(file); + var ext = ""; + if(file.originalname.includes(".")){ + ext = "." + file.originalname.split(".")[1]; + console.log(ext); + } + console.log(ext); + return cb(null, 'img-' + Date.now()+ext) + } + }) +var upload = multer({ storage: storage }) + const port = 80; const server = express(); +// server.use(bodyParser.json()); +server.use(bodyParser.urlencoded({ extended: true })); +server.use(function (req, res, next) { + console.debug("express:", req.method, req.originalUrl); + next() +}) + function listen(){ server.listen(port, () => console.info(`Listening on port ${port}!`)); @@ -11,6 +37,8 @@ function listen(){ function setUpRoutes(models){ server.get('/', (req, res) => res.sendFile(__dirname + "/html/index.html")) + server.get('/index', (req, res) => res.sendFile(__dirname + "/html/index.html")) + server.get('/admin', (req, res) => res.sendFile(__dirname + "/html/admin.html")); server.get('/bread', (req, res) => res.sendFile(__dirname + "/html/bread.html")); server.get('/essay', (req, res) => res.sendFile(__dirname + "/html/essay.html")); server.get('/snake', (req, res) => res.sendFile(__dirname + "/html/snake.html")); @@ -18,15 +46,13 @@ function setUpRoutes(models){ request(`http://localhost:8000?${req.url.split("?")[1]}`, function(error, response, body) { }); }) - server.get('/:type/posts.json', async (req, res, next) => { + server.get('/posts/:type', async (req, res, next) => { try { const { type } = req.params; - console.log(type); - console.log(req.params); - var posts = await models.posts.findAll({where: { type: type }}); + var posts = await models.posts.findAll({where: { type: type }, order: [['createdAt', 'DESC']]}); posts = posts.map(x => x.get({ plain: true })); for (const post of posts) { - const images = await models.pictures.findAll({ attributes: ["source"], where: { postId: post.id } }).map(x => x.source); + const images = await models.pictures.findAll({ attributes: ["source"], where: { postId: post.id }}).map(x => x.source); post.images = images; } res.status(200).send({ success: true, data: posts }); @@ -35,6 +61,23 @@ function setUpRoutes(models){ res.status(400).send({ success: false, error: e.message }); } }) + server.post('/posts', upload.array('images'), async (req, res, next) => { + try { + console.log(req.body); + + const type = req.body.type + const newPost = await models.posts.create(req.body); + req.files.forEach(async (file) => { + await models.pictures.create({"source": "uploads/"+file.filename, "postId": newPost.id}); + console.log("uploaded ", file.path); + }) + console.log(newPost); + res.redirect(`/${type}`); + next(); + } catch (e) { + res.status(400).send({ success: false, error: e.message }); + } + }) server.get('/favicon.ico', (req, res) => res.sendFile(__dirname + "/icon/favicon.ico")) @@ -42,8 +85,12 @@ function setUpRoutes(models){ res.sendFile(__dirname + "/css/"+req.params.id); }); server.get('/photo/:id', (req, res) => { + // res.setHeater("Content-Type", "image") res.sendFile(__dirname + "/photo/"+req.params.id); }); + server.get('/uploads/:id', (req, res) => { + res.sendFile(__dirname + "/uploads/"+req.params.id); + }); server.get('/essay/:id', (req, res) => { res.sendFile(__dirname + "/html/essay/"+req.params.id); }); |