Express starter
A REST API with Express 5: list, create, and delete todos.
Open in editor Free. Runs in your browser, no sign-up.
- express
Express
The code
Two files, exactly what you get in the editor.
import express from 'express'
type Todo = { id: number; title: string; done: boolean }
const app = express()
app.use(express.json())
let nextId = 3
const todos: Todo[] = [
{ id: 1, title: 'Write an API', done: true },
{ id: 2, title: 'Deploy it with jsdrop', done: false },
]
app.get('/', (_req, res) => {
res.type('html').send(`<h1>Todo API</h1>
<p>Try <a href="/todos">GET /todos</a>, or POST JSON like {"title": "Buy milk"} to /todos.</p>`)
})
app.get('/todos', (_req, res) => res.json(todos))
app.post('/todos', (req, res) => {
const title = String(req.body?.title ?? '').trim()
if (!title) return res.status(400).json({ error: 'A title is required.' })
const todo = { id: nextId++, title, done: false }
todos.push(todo)
res.status(201).json(todo)
})
app.delete('/todos/:id', (req, res) => {
const index = todos.findIndex((todo) => todo.id === Number(req.params.id))
if (index < 0) return res.status(404).json({ error: 'Not found.' })
res.json(todos.splice(index, 1)[0])
})
const port = Number(process.env.PORT) || 3000
app.listen(port, () => console.log(`Todo API on port ${port}`))
Questions
Can I run Express in the browser?
Yes. Press Run and the program runs in a Node.js-compatible runtime, compiled to WebAssembly, right in your browser tab. npm packages install in the browser too.
How do I start a Express project online?
Click Open in editor. jsdrop copies this starter into a new drop with index.ts and package.json. Press Run to start it, and edit as you go.
Can other people reach my server?
Yes. When the program calls listen(), the preview shows the server and a public tunnel URL that works while your tab runs it. With Pro, deploy the drop to run around the clock at its own address.
Is it free?
Yes. Public drops, running in the browser, live collaboration, versions, and embeds are free, and you do not need an account to start.