Phoenix官方教程 (一) 构建和运行

安稳与你 提交于 2019-12-07 15:53:57

首个教程的目的在于尽可能快地让一个Phoenix应用构建好并运行起来.

在我们开始前,请花一分钟阅读Installation Guide.安装好了所有必要的依赖,我们才能让应用流畅地构建和运行.

所以,我们需要安装Elixir, Erlang, Hex,和Phoenix.还需要安装好PostgreSQL和node.js用于构建一个默认应用.

Ok, 我们已经准备好了!

我们可以在任何目录中运行mix phoenix.new,来新建我们的Phoenix应用.Phoenix可接受目录的相对路径或绝对路径.假设我们的应用名为hello_phoenix,下列方法都可运作.

$ mix phoenix.new /Users/me/work/elixir-stuff/hello_phoenix
$ mix phoenix.new hello_phoenix

在开始前,我们要注意Brunch.io: Phoenix会默认使用Brunch.io进行资源管理.Brunch.io的依赖是通过node包管理工具安装的,而不是mix. Phoenix会在mix phoenix.new的末尾提示安装它们.如果我们选择no, 而之后没有通过npm install安装那些依赖,那么当我们试图启动应用时就会抛出错误,而且我们的资源可能没有合适地被载入。如果我们不想使用Brunch.io,我们可以简单地在mix phoenix.new之后加上--no-brunch

现在,让我们运行mix phoenix.new,使用相对路径。

mix phoenix.new hello_phoenix
* creating hello_phoenix/config/config.exs
* creating hello_phoenix/config/dev.exs
* creating hello_phoenix/config/prod.exs
...
* creating hello_phoenix/web/views/layout_view.ex
* creating hello_phoenix/web/views/page_view.ex

Fetch and install dependencies? [Yn]

Phoenix生成了目录结构,和我们的应用所需的所有文件。当完成后,它会询问我们是否需要安装依赖。让我们选择yes。

Fetch and install dependencies? [Yn] Y
* running mix deps.get
* running npm install && node node_modules/brunch/bin/brunch build

We are all set! Run your Phoenix application:

    $ cd hello_phoenix
    $ mix phoenix.server

You can also run your app inside IEx (Interactive Elixir) as:

    $ iex -S mix phoenix.server

Before moving on, configure your database in config/dev.exs and run:

    $ mix ecto.create

当依赖安装好后,它会提示我们进入项目文件夹,并启动我们的应用。

Phoenix假设我们的PostgreSQL数据库中有一个postgres用户账号,它有正确的权限且密码是postgres。如果情况不符合,请查阅mix任务ecto.create

Ok,让我们来试一下。首先,我们会cd到刚创建的hello_phoenix/目录:

$ cd hello_phoenix

现在,我们将创建我们的数据库:

$ mix ecto.create
The database for HelloPhoenix.Repo has been created.

注意:如果你是第一次运行这个命令,Phoenix可能会请你安装Rebar。请完成安装,Rebar是用于构建Erlang包的。

最后,我们将启动Phoenix服务器:

$ mix phoenix.server
[info] Running HelloPhoenix.Endpoint with Cowboy using http on port 4000
23 Nov 05:25:14 - info: compiled 5 files into 2 files, copied 3 in 1724ms

如果我们选择了不让Phoenix在生成新应用时安装依赖,那么phoenix.new任务会在我们想要安装它们时,提示我们执行必要的步骤。

Fetch and install dependencies? [Yn] n

We are all set! Run your Phoenix application:

    $ cd hello_phoenix
    $ mix deps.get
    $ mix phoenix.server

You can also run your app inside IEx (Interactive Elixir) as:

    $ iex -S mix phoenix.server

Before moving on, configure your database in config/dev.exs and run:

    $ mix ecto.create


Phoenix uses an optional assets build tool called brunch.io
that requires node.js and npm. Installation instructions for
node.js, which includes npm, can be found at http://nodejs.org.

After npm is installed, install your brunch dependencies by
running inside your app:

    $ npm install

If you don't want brunch.io, you can re-run this generator
with the --no-brunch option.

Phoenix默认在4000端口接收请求。如果我们在浏览器中输入http://localhost:4000 ,我们将看到Phoenix框架的欢迎页面。

Phoenix Welcome Page

如果你的屏幕看上去和图中一样,那么恭喜你!你现在有了一个正在运作的Phoenix应用。如果你看不到上面的页面,试着访问http://127.0.0.1:4000 并确认你的系统将“localhost”设定为“127.0.0.1”。

本地,我们的应用运行于一个iex会话中。我们要按两次ctrl-c来停止它,就像停止普通的iex那样。

下一步 我们将稍稍自定义我们的应用,了解一下一个Phoenix应用是如何拼接起来的。

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!