快速路线

text
阅读 40 收藏 0 点赞 0 评论 0

expressroutes.js
const express = require('express');
const app = express();
 
app.get("/hello", function(req, res){
    res.send("Hello!");
});

app.get("/", function(req, res){
    res.send("Welcome to my website!");
});
 
app.listen(3000, () => console.log('Example app listening on port 3000!'));
expressroutes.txt
Now we are going to be learning how to work with express routes. Routes are a way for the application to respond to certain tyes of url's. Most advanced websites use a form of routing or routing. Routing makes your web application dynamic, meaning that you can generate different variations of one html page. This itself basically means that instead of showing the same web page everytime a user loads a url from your website, you can show different forms of that webpage based on the information you give that webpage. It's super simple, but at the same time, super complex. 
That might have been a horrible explanation, so let me try and show you. If we go to https://www.kongregate.com/top-rated-games for example, notice that the url doesn't have an extension like you normally would when loading a simple HTML page. This is not exactly html. What the site basically does it send a bunch of information to the route called "top-rated-games" and the page will show you different information like different games that are popular on that day based on that data. It's all magical. Routes make your life EASY. 

Let's create our very first route! To do this, use the app constant and then call upon the get method. Look below to see how I did that. The get method will then need to take 2 parameters. First, it's the URL. For example, /hello. So like onlinereading.org/hello is how it would be seen. The next parameter is what's called a callback function. That is basically just what code is run when the first parameter is called. The callback function itself will take 2 arguments, req and res. They stand for Request and Response and you don't really need to know what they do yet, we will be using one right now and using them both later on extensively. But I'll give you a lil summary. Req and res are both objects and Req contains all the information about the route that was made. Response will return all of the information that we are going to respond with. Let's now do a basic response. If we use the res object and call the send method, we can send a message to the user everytime they go to /hello. It's really simple but pretty cool. Don't forget to look below to see how I did it. 

Don't forget to use node app.js to run your application. Every time you update your code, like a route, don't forget to restart your application. 

So as you hopefully can see, when you now run your application and go to localhost:3000/hello, it should send you the message you have as the res.send parameter. 

Now, you may have noticed that when you go to localhost:3000 without any route at the end, you get a little error. It says Cannot GET /. That means that there is no route for /. / is the "root" route, meaning the route that is run when you go to your url without any route at the end. Basically just your home page! Now, lets go ahead and add a route for that one too. 

Restart your application and see if you did it correctly. Now add as many routes as you want and play around with it. It's pretty cool. 

If you have any questions or edits I can add to this, leave a comment here or on my video. 
Video: https://www.youtube.com/watch?v=I2SQpKcJVm4&feature=youtu.be





评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号