Creating a Throwaway Project

Since C programmers wrote the first “Hello, World” examples in 1978, it’s traditionally been the first program you write when learning almost any language. So we don’t break with tradition, we’re going to create a “Hello, World” application as our first project. It will help you get your feet wet. When you’re done, you’ll get to see all of those layers we talked about in practice.

You now have a shiny new Phoenix installation. It’s time to build a project. You’re in a functional language, so you’re going to spend all of your time writing functions. This common project structure will help you organize things so you don’t have to reimagine it for each project.

In Elixir, repetitive tasks that manage the programming cycle will run in Mix. Each time you call this utility, you specify a task—an Elixir script—to run. Let’s use a task now to create our first Phoenix project, like this:

 $ ​​mix​​ ​​phx.new​​ ​​hello
 * creating hello/config/config.exs
 ...
 Fetch and install dependencies? [Yn] y
 * running mix deps.get
 * running mix deps.compile
 ...
 
 $ ​​cd​​ ​​hello
 $ ​​cd​​ ​​assets
 $ ​​npm​​ ​​install
 ...
 $ ​​cd​​ ​​..

We’re all set! We created a new project and built our static assets. At the bottom of the mix phx.new output, you can see a few sentences that tell you what to do next. Change into the hello directory and run the mix tasks to create the database and boot up the Phoenix web server, which will start looking for requests on port 4000.

Run your Phoenix application like this:

 $ ​​mix​​ ​​ecto.create
 $ ​​mix​​ ​​phx.server

Database errors

images/aside-icons/info.png

If you receive database errors when running mix ecto.create, double-check your Hello.Repo username and password values in config/dev.exs and match your system settings where necessary.

You can see that the server started on port 4000. The [info] blocks tell you exactly where this server is running. Point your browser to http://localhost:4000/. You can see a simple Phoenix welcome page.

images/src/getting_started/welcome.png

And we’re live! There’s no way we’re going to get a million-dollar valuation with this product, though. Let’s begin to change that by building our first feature.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.226.251.70