How to deploy express.js on Netlify

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
okay so we're gonna build a single API root on top of Express on top of metla Faiz functions let's get started so first off is a directory called net low fly Express second we're gonna go into that directory third we're gonna make package JSON file all the defaults and then we're going to install our dependencies we have Express a package called net with 5 lambda which will basically serve our functions locally and compile them for us for production and then a package called serverless HTTP which takes our Express app and converts it into a lambda ready to go okay now that those are installed let's open our folder and visual studio code packages installed let's quickly make a git ignore file so we could ignore these node modules and while we're here we'll also have to ignore our compiled functions and we're going to be putting those inside a directory called functions so I'll ignore that now next off we all setup net with I so we'll do a net Liffe I dot Tamil file and in our build step we will Klara functions to go in our directory called functions yes that's all we need here and I think the last thing we need for net left eye is a new folder called dist and inside there we're just going to put a blank file okay so every time you deploy something to netwo fie you need to give it a district turi so we're just going to give it a dist directory with just an empty file in it okay so then the next thing we want to do is create our source directory and create our API so we're naming a J's file called API and then we're going to bring in Express so we're going to require Express and we're going to bring in server lists and remember that this is at the serverless HTTP server less HTTP package and then we can create our Express app like one or Mele would Oh new instance of Express and then what we want to do is in order for the lambda to run we need to export a handler function so we'll do module that exports not handler equals and then we're gonna take our Express app and wrap it with serverless so serverless and then pass in our app and then lastly all we need to do is make a router and create our route so new router equals Express start router our route can look like router dot get and we'll pass in our request and a response and we will return let's just return some JSON so we'll do like Hello with a high value and then we need to take this a router that we've made and bind it into the app so we'll do F dot use router however we need to tell the router where it's bound to and what net Liffe I does is it'll put all of your functions inside this dotnet Wi-Fi directory and then inside whatever you passed in for your functions parameter so in our cases functions and then in our case we need to do the name of the function which is API so now the router knows that it lives at this route and then it'll serve everything from this and then here okay so now the last thing we need to do is create our two commands one just start our lambda locally and want to build it in production so in package JSON make one called script called start and that'll be the netwo 5 lambda package that we installed locally with the serve command and we're going to serve the source directory and then we're going to do the same thing for build but instead of serve we're gonna do build source directory now we should be able to run NPM start this from here and then I'll start our server and run everything locally on port 9000 let me go localhost 9000 and then remember that our app the router of our app is bound to netwo five functions API so we need to hit that root we hit that route and we got our hello hi JSON output and then from here we can take it further and if we wanted to add extra endpoints we can make another one called test and change our value to be test and you'll notice too that nella Phi lambda which is building our app will actually watch for changes and rebuild the app on the fly so we don't have to restart the server we can just go to our new route and we get test so now the next thing we want to do is deploy this to nullify so I have this example app set up and running using the same code base our build command is just NPM run build and then like I mentioned previously we need always need to specify a publish directory so that's why we have that dist with the empty index.html and then when you hit that app it just returns that empty page but then you can go to donnatella phi functions api and we get our JSON output okay so this has been how you can run express identify using that LaFace functions hope you enjoyed the video if you did please leave a like subscribe to the channel and follow me on Twitter at Owen County
Info
Channel: Owen Conti
Views: 121,422
Rating: undefined out of 5
Keywords: web, development, programming, coding, computers, netlify, expressjs, serverless, netlify functions, cloud, functions, serverless-http, netlify-lambda
Id: hQAu0YEIF0g
Channel Id: undefined
Length: 5min 23sec (323 seconds)
Published: Sat Mar 30 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.