Node.js routes with Angular.js

Asked

Viewed 176 times

0

I am starting now in development with Ode and came to me the following doubt: much is said about the mean stack, but how to map routes using Node and angular? Which one will I use to make the routes? Could you give an example? Being that with the two there is the possibility of making calls of the methods. Thanks for the help!

1 answer

0

In MEAN Stack Mongo is used to save information, NodeJs to the back-end, express to help in the development Nodejs, and angularJs to the front-end, that is, the Angularjs will take care of the management of the routes, and the Node will only map them, to return the result according to the route, example...

Build with express

var express = require('express');
var bodyParser = require('body-parser');
var app = express();

app.use(express.static(__dirname + '/www'));
app.use(bodyParser());

var porta = 3412;

app.listen(process.env.PORT || porta, function(){
    console.log('Servidor online porta ' + porta);
});

Mapping the routes

app.get('/nomeDaRota', function(req, res) {
  res.json(restaurantes);
});

app.get('/nomeDaRota/:id', function(req, res) {
    var id = req.params.id;

    for (var i = 0; i < objeto.length; i++) {
        if (objeto[i].id === parseInt(id)) {
                var peg = objeto[i];
        }
    }

    if (typeof peg == "undefined") {
        res.json('Erro');
    }else{
        res.json(peg);
    }

});

Browser other questions tagged

You are not signed in. Login or sign up in order to post.