Skip to content

Commit

Permalink
Swap express for polka.
Browse files Browse the repository at this point in the history
  • Loading branch information
pngwn committed Jun 14, 2019
1 parent 622f285 commit 6da6cf3
Show file tree
Hide file tree
Showing 10 changed files with 89 additions and 30 deletions.
3 changes: 3 additions & 0 deletions .netlify/state.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"siteId": "c80398a1-fd2e-4698-8373-988dc10d95cc"
}
10 changes: 10 additions & 0 deletions netlify.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[build]
publish = "__sapper__/export"
command = "yarn export"
functions = "src/lambda/"

[dev]
functions = "src/lambda/"
command = "yarn dev" # Command to start your dev server
port = 3000 # Port that the dev server will be listening on
publish = "__sapper__/dev"
51 changes: 51 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@
"express-session": "^1.16.2",
"marked": "^0.6.2",
"node-fetch": "^2.1.2",
"polka": "^0.5.2",
"sapper": "^0.27.4",
"serve-static": "^1.13.1",
"session-file-store": "^1.2.0",
"sirv": "^0.4.2",
"svelte-extras": "^2.0.2"
},
"devDependencies": {
Expand Down Expand Up @@ -52,4 +54,4 @@
"yarn.lock"
]
}
}
}
5 changes: 1 addition & 4 deletions src/routes/auth/login.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@ export function post(req, res) {

api.post('users/login', { user }).then(response => {
if (response.user) req.session.user = response.user;
console.log(response.user);
res.set({
'Content-Type': 'application/json'
});
res.setHeader('Content-Type', 'application/json');

res.end(JSON.stringify(response));
});
Expand Down
4 changes: 1 addition & 3 deletions src/routes/auth/register.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ export function post(req, res) {
req.session.user = response.user;
}

res.set({
'Content-Type': 'application/json'
});
res.setHeader('Content-Type', 'application/json');

res.end(JSON.stringify(response));
});
Expand Down
4 changes: 1 addition & 3 deletions src/routes/auth/save.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ export function post(req, res) {
req.session.user = response.user;
}

res.set({
'Content-Type': 'application/json'
});
res.setHeader('Content-Type', 'application/json');

res.end(JSON.stringify(response));
});
Expand Down
4 changes: 1 addition & 3 deletions src/routes/auth/user.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
export function get(req, res) {
res.set({
'Content-Type': 'application/json'
});
res.setHeader('Content-Type', 'application/json');

res.end(JSON.stringify({ user: req.session.user || null }));
}
17 changes: 7 additions & 10 deletions src/routes/editor/_Editor.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,24 @@
<div class="row">
<div class="col-md-10 offset-md-1 col-xs-12">
<ListErrors {errors}/>

<form>
<fieldset>
<fieldset class="form-group">
<input class="form-control form-control-lg" type="text" placeholder="Article Title" bind:value={article.title}>
</fieldset>

<fieldset class="form-group">
<input class="form-control" type="text" placeholder="What's this article about?" bind:value={article.description}>
</fieldset>

<fieldset class="form-group">
<textarea class="form-control" rows="8" placeholder="Write your article (in markdown)" bind:value={article.body}/>
</fieldset>

<fieldset class="form-group">
<input class="form-control" type="text" placeholder="Enter tags" use:enter='{addTag}'>

<div class="tag-list">
{#each article.tagList as tag, i}
<span class="tag-default tag-pill">
Expand All @@ -30,7 +30,7 @@
{/each}
</div>
</fieldset>

<button class="btn btn-lg pull-xs-right btn-primary" type="button" disabled={inProgress} on:click='{publish}'>
Publish Article
</button>
Expand All @@ -48,13 +48,11 @@
export let params, article;
console.log(params)
let inProgress = false,
errors;
const { session } = stores();
function addTag(input) {
console.log(article)
article.tagList = article.tagList.concat(input.value);
input.value = '';
}
Expand All @@ -66,7 +64,6 @@
function publish() {
inProgress = true;
console.log(!params.slug, $session.user);
const promise = !params.slug ?
api.post('articles', { article }, $session.user && $session.user.token) :
api.put(`articles/${params.slug}`, { article }, $session.user && $session.user.token);
Expand All @@ -77,7 +74,7 @@
}
});
}
function enter(node, callback) {
function onkeydown(event) {
Expand Down
17 changes: 11 additions & 6 deletions src/server.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
import express from 'express';
import sirv from 'sirv';
import polka from 'polka';
import compression from 'compression';
import serve from 'serve-static';
import bodyParser from 'body-parser';
import session from 'express-session';
import sessionFileStore from 'session-file-store';
import * as sapper from '@sapper/server';
// import { Store } from 'svelte/store.js';

const FileStore = sessionFileStore(session);

express()

const { PORT, NODE_ENV } = process.env;
const dev = NODE_ENV === 'development';

polka()
.use(bodyParser.json())
.use(session({
secret: 'conduit',
Expand All @@ -24,11 +27,13 @@ express()
}))
.use(
compression({ threshold: 0 }),
serve('static'),
sirv('static', { dev }),
sapper.middleware({
session: req => ({
user: req.session && req.session.user
})
})
)
.listen(process.env.PORT);
.listen(PORT, err => {
if (err) console.log('error', err);
});

0 comments on commit 6da6cf3

Please sign in to comment.