Defining a Server
How to define and start the server for a Joystick app.
In the index.server.js
file at the root of your project (created for you when you ran joystick create
), we call the .app()
method on the joystick
default export from the @joystick.js/node
package:
Example Server Definition
import joystick from "@joystick.js/node";
import api from "./api/index.js";
import books_fixture from './fixtures/books.js';
joystick.app({
api,
fixtures: () => {
books_fixture();
},
routes: {
"/": (req = {}, res = {}) => {
res.render("ui/pages/index/index.js", {
layout: "ui/layouts/app/index.js",
});
},
"*": (req = {}, res = {}) => {
res.render("ui/pages/error/index.js", {
layout: "ui/layouts/app/index.js",
props: {
status_code: 404,
},
});
},
},
});
When you start your app via the joystick create
command, this file will be loaded and joystick.app()
will start your HTTP server via Express.
Extending our example from the previous lesson, above, we've added the fixtures
option to the options we pass to joystick.app()
(as we'll see, these define the behavior of our Joystick app's server). On startup, fixtures()
will be called internally by Joystick and by extension, our books_fixture()
function will be called, seeding our database with test data.