GETTING STARTED

MAIN CONCEPTS

API

Quick start

Tuft supports Node.js version 12 and higher. If you're running an earlier version of Node, please update before continuing.

Install Tuft via npm.

$ npm install tuft

Tuft has no default export. Instead, you should start by importing the named function tuft.

const { tuft } = require('tuft')

You can then invoke it to create a Tuft "application", which allows you to add routes.

const app = tuft()

app.set('GET /', { text: 'Hello, world!' })

The above code will add a single route that responds with the text 'Hello, world!' to any GET requests for the path '/'.

Once you've finished adding your routes, create a server instance and call its .start() method.

const server = app.createServer({ port: 3000 })
server.start()

Tuft is a promise-based framework, and the .start() method in the above code actually returns a promise, so you should wait for the promise to resolve if you want to know the server is actually running.

async function startServer() {
  const { host, port } = await server.start()
  console.log(`Server listening at http://${host}:${port}`)
}

startServer()

You can get the same result as above by chaining everything together:

const { tuft } = require('tuft')

tuft()
  .set('GET /', { text: 'Hello, world!' })
  .createServer({ port: 3000 })
  .start()
  .then(({ host, port }) => {
    console.log(`Server listening at http://${host}:${port}`)
  })

Congratulations! You now have a working "Hello, world!" Tuft server.