This chapter covers:
- Server routes
- Server route source files
- Server route functions
- Working Create/Retrieve/Update/Delete (CRUD) example
- Switching to Express
Server routes are Node-based API/REST services. Sapper supports implementing these and it hosts them so the client-side code can send requests to them. It enables collocating server-side code with client-side code in the same project. This is convenient for full stack developers because it avoids having to maintain multiple source code repositories, one for client-side code and one for server-side code. It also has the benefit of using the same programming language, JavaScript, for all of the code.
Note
It is possible to host a Svelte app and Node-based API server in the same repository (a monorepo) without using Sapper. However, in that case they would typically be implemented in separate source trees, each with their own package.json
file. With Sapper, the front-end and back-end code are in the same source tree and the same HTTP server is used to serve front-end assets and respond to API requests.
Server route source files define functions for server-side code that is part of a Sapper application. This code can perform tasks like managing data persistence using a relational database such as PostgreSQL or a NoSQL database such as MongoDB. For example, an app that manages employee data can use API services to create, retrieve, update, and delete this data.