Afrikaans
Akan
Albanian
Amharic
Arabic
Armenian
Azerbaijani
Basque
Belarusian
Bemba
Bengali
Bihari
Bosnian
Breton
Bulgarian
Cambodian
Catalan
Cebuano
Cherokee
Chichewa
Chinese (Simplified)
Chinese (Traditional)
Corsican
Croatian
Czech
Danish
Dutch
English
Esperanto
Estonian
Ewe
Faroese
Filipino
Finnish
French
Frisian
Ga
Galician
Georgian
German
Greek
Guarani
Gujarati
Haitian Creole
Hausa
Hawaiian
Hebrew
Hindi
Hmong
Hungarian
Icelandic
Igbo
Indonesian
Interlingua
Irish
Italian
Japanese
Javanese
Kannada
Kazakh
Kinyarwanda
Kirundi
Kongo
Korean
Krio (Sierra Leone)
Kurdish
Kurdish (Soranรฎ)
Kyrgyz
Laothian
Latin
Latvian
Lingala
Lithuanian
Lozi
Luganda
Luo
Luxembourgish
Macedonian
Malagasy
Malay
Malayalam
Maltese
Maori
Marathi
Mauritian Creole
Moldavian
Mongolian
Myanmar (Burmese)
Montenegrin
Nepali
Nigerian Pidgin
Northern Sotho
Norwegian
Norwegian (Nynorsk)
Occitan
Oriya
Oromo
Pashto
Polish
Portuguese (Brazil)
Portuguese (Portugal)
Punjabi
Quechua
Romanian
Romansh
Runyakitara
Russian
Samoan
Scots Gaelic
Serbian
Serbo-Croatian
Sesotho
Setswana
Seychellois Creole
Shona
Sindhi
Sinhalese
Slovak
Slovenian
Somali
Spanish
Spanish (Latin American)
Sundanese
Swahili
Swedish
Tajik
Tamil
Tatar
Telugu
Thai
Tigrinya
Tonga
Tshiluba
Tumbuka
Turkish
Turkmen
Twi
Uighur
Ukrainian
Urdu
Uzbek
Vietnamese
Welsh
Wolof
Xhosa
Yiddish
Yoruba
Zulu
We've now done some initial setup and now it's time to actually start writing out our services.
But this initial implementation we're not going to worry too much about anything micros services or
anything like that.
We're really just going to put together some very basic functionality for the posts in comment service.
So in other words some very basic express J.S. code to manipulate or deal with some very basic resources.
We're going to first begin with our post service.
Whenever you start designing a service or putting it together it really is worth a time to think very
critically about exactly what you want your service to do.
So in our case we know that we want to create a post and list all posts.
Well let's get a little bit deeper technical detail here and really truly understand what this thing
needs to do.
So here's my thought for the Postal Service.
I think that we need to have a single route that's going to be in charge of retrieving all the different
posted have ever been created.
I think we also need one distinct route that is can be in charge of creating a brand new post.
We're going to have one route of us if we make a GET request to it we'll get all of our posts.
We'll have another one of slash posts if we make a post to it with somebody that has a title that it
is a string.
We're going to create a new post.
So that's what we're going to use as our guide for implementing our post service.
Let's open up our code editor inside the Post's project directory right now.
We're going to start to build out a very small express application that's going to implement these two
routes back at my terminal.
You'll notice I've redone my layout here just you can see these different terminal windows a little
bit more easily I'm going to find my post terminal window and open up my code editor inside their
and then once inside they're going to do a initial file I'm going to make right away of index that J.S.
we're gonna write out just about all the code for application inside this one single file at the very
top.
We're going to get access to express we're gonna create new express application we're gonna set up those
two different route handlers so we just discussed it's all gonna ease some pretty straightforward express
related stuff so let's get to it a little bit of typing here.
So first off we will require an express I'll create a new app
and then I'm going to associate those two different routes with the app we just created.
So we'll do an app get to slash posts and I'll put in my root handler and then if we ever make a post
request to slash posts and we'll put in rec and rez as well then at the bottom right away I'm going
to make sure that my express application listens on a very specific port so I'm gonna put in here app
dot listen we're gonna have this first service listen on Port 4000 and then inside that callback we'll
say console log listening on 4000.
All right.
Simple enough okay.
So now let's start to implement these two different root handlers I mentioned in passing just a moment
ago in previous video that we're not going to worry about any database or anything like that with these
different services.
So we're going to store all these different resources in memory.
The downside to this approach is that anytime that we restart a service we're going to lose all of our
data.
But that's totally okay for this initial little toy project we're putting together right after I create
my express application I'm going to make a little object that's going to be in charge of storing all
the different posts that get created.
We'll say concert us is an empty object like so that this object is where we're going to store every
post that we create now that we've got this kind of repository for all the different post we're going
to store.
We can start to implement the first row handle right here.
That's going to retrieve all the different posted have been created very easily in just say inside that
root handler.
Let's send all of our posts.
Now if anyone makes a request to slash posts we're gonna send back all the posts have been created the
next we have to do is make sure that we implement the ability to create a new post.
So we have to add in some implementation for the post request handler to slash posts.
Remember that we probably want to eventually have some ideas associated with every post that gets created.
Right now we don't really have any ability to create a idea of any sort.
So let's add in a little bit of code to randomly generate an idea whenever someone makes a post request
to slash post right here with the intent to create a post.
We're going to randomly generate an I.D. assign it to the object that the user just sent to us and then
store that entire thing.
That's a little bit confusing for me to say.
So let me just show you exactly what we're going to do at the very top of the file.
I'm going to add in an additional require statement.
I'm going to require in random bytes from the crypto package.
We're going to use random bytes right here to generate a new idea that we're going to assign to the
posts that the user is trying to create.
So down inside the post request handler and say const I.D. We're gonna generate our random idea right
here and we'll do that by using the random bytes function we'll say random bytes.
I want four bytes of random data.
So this is just you get a random string for us that's all we're doing here.
We'll say to string x.
So that's going to give us a nice random looking I.D. It looks something like that.
It'll be actually x.
So we won't have like k's in there and ages and whatnot but you get the idea that's pretty much what
we're going to have for our I.D. It's now the next you're going to do is take a look at the request
that we've just received from the user.
Remember we're making the assumption here that whenever a user makes a post request to this route they're
going to send along a body that has a title property inside of it.
So we're going to pull out that title.
We're going to generate that or take that randomly generated I.D. and then store them altogether inside
of our post object up here that we just created so let's get access to the title that the user just
sent to us.
Let's say Rick dot body will then add in a new e at the idea that we just generated on our posts object
and I'll assign to that the idea and the title and then finally it down here at the bottom after we
add in that brand new post let's send back a response to the user to let them know that hey we have
now reached a new post for you.
It's all good to go.
I'll do a rez set a manual status here of 201 which indicates we just created a resource and then I'll
send back those at I.D. which again is the post that we just created.
All right.
So this is looking pretty good.
There's one last little thing we have to do we need to make sure that we add in a body parser to make
sure that whenever a user sends us some Jason data in a body request actually gets passed.
And so it actually shows up appropriately inside of a request handler.
So just a bit of administration work with Express J.S. at the very top the file I'm going to add in
a required statement for body parser
and then right after we generate our app we'll do an app dot use body parser dot.
Jason like so okay.
And I should be at the code that we have here should be good to go.
It implements both of these different features or these routes that we just discussed.
The last thing we should do is add in a script to our package not just on file to start this project
up.
We can then take a quick break and then do some testing this thing in the next video.
Go inside my package dot Jason File I'm gonna find the script section.
I'm going to remove the default script inside their and I'll replace it with a start script that will
run node on index dodges like so I'll save that.
And then finally I'm going to go back over to my terminal I'll find my post terminal window and I'll
do an NPM start inside their and I should see something says listening on 4000.
Very good.
Okay.
So now that we've got this thing up and running our first little service Rick pause right here and we'll
do a quick test of this thing in the next video just to make sure that's working as expected.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.