Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
Bruno Boiget
svelte-test
Commits
f846d5be
Commit
f846d5be
authored
Oct 22, 2020
by
Bruno Boiget
Browse files
make blog list updateable
parent
840bfcd8
Changes
3
Hide whitespace changes
Inline
Side-by-side
src/routes/blog/[slug].json.js
View file @
f846d5be
import
posts
from
'
./_posts.js
'
;
import
{
posts
}
from
'
./_posts.js
'
;
const
lookup
=
new
Map
();
posts
.
forEach
(
post
=>
{
lookup
.
set
(
post
.
slug
,
JSON
.
stringify
(
post
));
posts
.
forEach
(
(
post
)
=>
{
lookup
.
set
(
post
.
slug
,
JSON
.
stringify
(
post
));
});
export
function
get
(
req
,
res
,
next
)
{
// the `slug` parameter is available because
// this file is called [slug].json.js
const
{
slug
}
=
req
.
params
;
// the `slug` parameter is available because
// this file is called [slug].json.js
const
{
slug
}
=
req
.
params
;
const
blogPost
=
posts
.
find
((
p
)
=>
p
.
slug
===
slug
);
if
(
lo
okup
.
has
(
slug
)
)
{
res
.
writeHead
(
200
,
{
'
Content-Type
'
:
'
application/json
'
});
if
(
b
lo
gPost
)
{
res
.
writeHead
(
200
,
{
'
Content-Type
'
:
'
application/json
'
,
});
res
.
end
(
lookup
.
get
(
slug
));
}
else
{
res
.
writeHead
(
404
,
{
'
Content-Type
'
:
'
application/json
'
});
res
.
end
(
JSON
.
stringify
(
blogPost
));
}
else
{
res
.
writeHead
(
404
,
{
'
Content-Type
'
:
'
application/json
'
,
});
res
.
end
(
JSON
.
stringify
({
message
:
`Not found`
}));
}
res
.
end
(
JSON
.
stringify
({
message
:
`Not found`
,
})
);
}
}
src/routes/blog/_posts.js
View file @
f846d5be
...
...
@@ -7,7 +7,7 @@
// we don't want to create an `/blog/posts` route — the leading
// underscore tells Sapper not to do that.
cons
t
posts
=
[
export
le
t
posts
=
[
{
title
:
'
Hello Dijon
'
,
slug
:
'
hello-dijon
'
,
...
...
@@ -96,5 +96,3 @@ const posts = [
posts
.
forEach
((
post
)
=>
{
post
.
html
=
post
.
html
.
replace
(
/^
\t{3}
/gm
,
''
);
});
export
default
posts
;
src/routes/blog/index.json.js
View file @
f846d5be
import
posts
from
'
./_posts.js
'
;
import
{
posts
}
from
'
./_posts.js
'
;
const
contents
=
JSON
.
stringify
(
posts
.
map
((
post
)
=>
{
...
...
@@ -13,7 +13,7 @@ export function get(req, res) {
res
.
writeHead
(
200
,
{
'
Content-Type
'
:
'
application/json
'
,
});
res
.
end
(
conten
ts
);
res
.
end
(
JSON
.
stringify
(
pos
ts
)
)
;
}
export
function
post
(
req
,
res
)
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment