D
D
deadkEEper12015-09-06 14:31:07
Node.js
deadkEEper1, 2015-09-06 14:31:07

How to properly organize routing on node?

Hello. I think I didn't fully understand the basics of routing, I know for sure that this is a bug, but I can't figure out how to do it right.
As far as I know, .../name and .../age should be query params How to do this? Thanks for any help

var express = require('express')
var app = express();

var me = {name : 'jura', age: 22}

app.get('/', function(req, res){
           res.send('welcome to homepage');
           res.end();
})

app.get('/user', function(req, res){
           res.send(me);
           res.end();
})

app.get('/user/name', function(req, res){      /*  уверен что зделал плохо. Какие методы использовать чтобы грамотно описать маршрутизацию *\
           res.send(me.name);
           res.end();
})

app.get('/user/age', function(req, res){        /.....
           res.send(me.age);
           res.end();
})

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Vapelnik, 2015-09-06
@deadkEEper1

I think a parameterized route should be used:

app.get('/user/:field', function(req, res){
    console.log(req.params.field);
//.....

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question