简体   繁体   中英

NodeJS - RESTful API - Python/JSON - Trigger

I'm working on a little project - completely new to this stuff - and hope you can help me out.

On the base of this project https://scotch.io/tutorials/build-a-restful-api-using-node-and-express-4 which is up running and works really great.

The main goal is it to send a UserID over the network - which is already working by python/json - and let the server react on this and show a login with the just sent UserID and an password input.

I already enhanced the server.js with a POST from the python script and when the server is running the terminal recognizes when the UserID is sent.

...
    router.route('/bears/:id')

      .get(function(req, res) {
        Bear.find({‚id': req.params.id}, function(err, bear) {
            if (err)
                res.send(err);
            console.log(‚here i am ...');
        });
    });
...

I also created a HTML which I thought should always listen (by AJAX) to the post and if the trigger (UserId) is send it should forward to another side with the input field.

So right now I don't really know how to implement that the Server already got the trigger and the HTML forwards the User to the Login?

Let the server handle where the user goes - issuing a 200 for a successful user, or an error 400 if it's a bad request. For example:

router.route('/bears/:id')

.get(function(req, res) {
    Bear.find({‚id': req.params.id}, function(err, bear) {
        if (err)
            res.status(400).send(err);    //Send a generic bad request error
        else {
            res.status(200).render('login', { name: req.params.id }); //Direct client to your login page (this example uses the jade template egine)
    });
});

I hope this is what you're looking for :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM