简体   繁体   中英

Node.js / Express routing doesn't work properly

First of all, I'm very new to Node.js. Here is thing; I'm making a mobile application which I need to use routes to login or register. On the android side, I'm using android volley library to accomplish post method. Here is my code on the server side.

server.js;

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

    // configure app to use bodyParser() which will get the data from a POST
    app.use(require('express-method-override')());
    app.use(bodyParser.json());
    app.use(bodyParser.urlencoded({ extended: true }));
    app.use(validator);
    app.use(express.static(__dirname + '/public'));

    var http = require('http');
    var ip = process.env.OPENSHIFT_NODEJS_IP || '127.0.0.1' || 'localhost',
        port = process.env.OPENSHIFT_NODEJS_PORT || '8080';

    // REGISTER ROUTES
    app.use('/', require('./routes').router);

    http.createServer(function (req, res) {
      res.writeHead(200, {'Content-Type': 'text/plain'});
      res.end('Hello World\n');
    }).listen(port, ip);

    console.log('Server running at http://'+ip+':'+port+'/');  

and routes.js

var router     = require('express').Router(),
    db         = require('./connectdb')(),
    formidable = require('formidable'),
    fs         = require('fs-extra'),
    util       = require('util'),
    path       = require('path');

router.use(function(req, res, next) {
    console.log('--new request--');
    next(); // visit next routes
});


//ANDROID General routes
router.get('/andro', function(req, res) {
    res.json({ message: 'Login/RegisterSystem!' });   
});


// LOGIN Route
router.route('/andro/login').post(function(req, res) {
    var tag      = req.body.tag,
        email    = req.body.email,
        password = req.body.password;

    console.log(email);
    console.log(password);

});

module.exports.router = router;

I'm watching the server logs but I couldn't see any log, when I used the domainname.com/andro/login or other defined routes with the methods.

log

Server running at http://127.12.211.129:8080/

Any approach would be awesome. Maybe I haven't understood how the routes work. How should I understand routes are working properly?

http.createServer(function (req, res) {
      res.writeHead(200, {'Content-Type': 'text/plain'});
      res.end('Hello World\n');
    }).listen(port, ip);

in fact, you create a http server has nothing to do with the app created by express();

http.createServer(app).listen(port, ip);

or simply

app.listen(port, ip);

Use Sleek.js MVC framework of node.js.

It's easy to manage your application code

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