garj

garj

Phoenix app with both html and json end point

Hi,

My phoenix app currently supports server side rendering. I would like it to support json end point as well. I know I can easily do this in router.ex by adding a scope: “/api/” and pipe_through api plug. However I do not know if I should create seperate controllers for the json app or I should modify existing controllers and render different view depending on the request. Any convention or thoughts related to this? Thanks!

Most Liked

OvermindDL1

OvermindDL1

You don’t need to pattern match in the controller. Phoenix already handles different views based on what the browser requests. For example when I do render(conn, :request, blah_args: blah_args) then it will render my "index.html" template called via render if html is requested, or it will call my render function that is matched on "index.json" for the JSON conversion (I also have others too). :slight_smile:

hubertlepicki

hubertlepicki

From my experience, the same controllers returning JSON view only works when the app is really simple CRUD-type of thing.

The needs of API client (either JS or mobile) are usually more specific, and you may want to optimize how and what data is being sent to those clients. This could result in messy controller code.

Go with the same controllers only when the API and pages you show match really well, and the thing is really simple.

If in doubt - build separate controllers. This does not hurt that much, sometimes more code is better than less - but convoluted code.

lukerollans

lukerollans

Hey there!

In my opinion, you should definitely create separate controllers for your API pipeline. As you’ve already said, setup your router to scope on /api or similar and pipe_through :api, where your api pipeline consists of at least plug :accepts, ["json"]

The alternative would be to pattern match on conn in a single controller and either render HTML or JSON views depending on the format request header or something. To me, this mixes concerns a bit too much and makes your pipelines much less flexible.

Where Next?

Popular in Questions Top

albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call t...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New

Other popular topics Top

senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New
chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 53690 245
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 39297 209
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 47930 226
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New

We're in Beta

About us Mission Statement