Zero abstracts the usual project configuration for routing, bundling, and transpiling to make it easier to get started.
It allows you to build your application without worrying about package management or routing. Write your code in a mix of Node.js, React, HTML, MDX, Vue, Svelte, Python, and static files and put them all in a folder.
Why Zero
Your project folder doesn't require config files. You just place your code and it's automatically compiled, bundled and served.
If your code resides in ./api/login.js
it's exposed at http://domain.com/api/login. Inspired by good ol' PHP days.
If a file does require('underscore')
, it is automatically installed and resolved. You can always create your own package.json file to install a specific version of a package.
Getting Started
Installation
You can install zero
globally by:
npm install -g zero
Hello World
Let's start by making a website that tells us server time.
First we need to create an API endpoint in Node.js to tell us time in JSON.
Create a new folder and add a new file time.js
in that folder. In this file, export a function that accepts Request
and Response
objects (like Express):
// time.jsconst moment = require("moment")module.exports = (req, res) => {var time = moment().format('LT'); // 11:51 AMres.send({time: time })}
Once saved, you can cd
into that folder and start the server like this:
zero
Running this command will automatically install any dependencies (like momentjs here) and start the web server.
Open this URL in the browser: http://localhost:3000/time
You just created an API endpoint 🎉:
Keep the server running. Now let's consume our API from a React page, create a new file index.jsx
and add the following code:
// index.jsximport React from 'react'export default class extends React.Component {static async getInitialProps(){var json = await fetch("/time").then((resp) => resp.json())return {time: json.time}}render() {return <p>Current time is: {this.props.time}</p>}}
This is a standard React component. With one additional hook for initial data population:
getInitialProps
is an async
static method which is called by zero
when the page loads. This method can return a plain object which populates props
.
Now go to this URL: http://localhost:3000/
and you should see the current server time rendered by React while fetch
-ing an API endpoint you created earlier:
zero
automatically bundles your code and supports server-side rendering. You don't need to fiddle with webpack anymore.
That's it! You just created a web application.