38

Introducing `now dev` – Serverless, on localhost

 5 years ago
source link: https://www.tuicool.com/articles/hit/A3iqqaY
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Now was born out of the idea that deploying a website could be much simpler. You only have to run a single command: now – that is all.
With ourGitHub andGitLab integrations, we enabled deploying on every git push , and teams to manage staging and production by simply merging pull requests.

Not only didNow 2.0 make deployments effortless, it made them scalable and global by default. Thanks to monorepo support, you could develop static websites andserverless lambdas within the same repository.

Today, we are excited to announce one command to run it all on localhost: now dev .

Typically, developing a website or application involves a series of steps:

  • Cloning repositories
  • Fetching packages
  • Synchronizing and matching ports
  • Downloading the right runtime for each programming language
  • And so on...
Even for modern projects, we often need to look up the README.md file for instructions about installing dependencies, and then tell a package manager to run developer scripts.

Now gives you a different contract. It is always now dev .

When you work with servers, the developer workflow is quite tedious. You have to run a process, find the port, then make your changes, then kill the process (usually with Control+C)...

When you work with modern frontend frameworks (like Next.js or Gatsby) your HTML, CSS and client-side JavaScript, hot-reloading is expected instead.

When you introduce API endpoints written as serverless functions, we now also have the advantage of bringing live reloading to your backend. now dev ensures that every single request builds and runs the latest version of your code (yes, like .php for every language).

Every piece of technology, from the client to the Builders that compile your code into static files and lambda functions, is fully open-source and MIT licensed.

Now ensures that you write code with the same open and standard APIs of your programming language or framework of choice.

Whether it is Next.js pages, Node.js HTTP request handlers or Express.js endpoints – we ensure that your code remains agnostic and portable.

Unlike the next dev command (the old way of developing Next.js apps), now dev allows you to run your entire suite of web apps and APIs all at once, with just a single command.
For example, nextjs-news uses both a Next.js application and a Node.js API – both running at the same time using now dev :

No matter how many builds, `now dev` will boot them all

If you are building a static site using a tool like Gatsby , all you need to do is add the command you would normally use to a now-dev script in package.json ...
{
  "scripts": {
    "now-dev": "gatsby develop -p $PORT"
  }
}
...and add a now.json file that contains the@now/static-build Builder:
{
  "version": 2,
  "builds": [{
    "src": "package.json",
    "use": "@now/static-build",
    "config": { "distDir": "public" }
  }]
}
Once that is completed, the tool (in this case gatsby develop ) is invoked automatically, along with all other Builders.

The Gatsbygram example showcases this really well:

@now/static-build is the only Builder that requires adjustment to work with `now dev`

If you chose to write a backend in Go , there are no changes to be made in order for your project to work with now dev , you just run the command:

In this example, both `@now/static` and `@now/go` are run in parallel

Even if your application is spawning a instance of Headless Chrome (like puppeteer ), local development will be very smooth, as now dev emulates production closely:

As you can see, both `now dev` and `puppeteer` will boot up in no time

As a bonus, you will be glad to know that now dev also acts as a substitute for serve out of the box. Any time you want to serve a directory quickly, you know what to reach for:

Even though there is no `now.json` file, serving the directory just works

now dev is available in beta as part of the latest version ofNow CLI. To get it, run npm install -g now or check out our otherdownload options.
If you need a quick project to test it with, try now init :
now init nextjs
cd nextjs

and then...

now dev

For more details, check outits documentation includingthe list of what official builders are already supported.

!

This feature release is limited toNow 2.0.

We want to hear from you and have set up adedicated feedback form to learn more about the different ways in which you would like to see now dev improve: Which technologies, platforms and frameworks you would like us to support, or any hiccups or slow paths you run into.
Furthermore, we also set up aFAQ page containing several common questions that might come up when using now dev and their answers.

And as always, you are invited tojoin our community and drop the team a note.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK