1

I created a NodeJS application with port 7777.

server.js

'use strict';
var express = require('express'),
    router = require('./router');
var app = express();
var port = 7777;
app.use(router());
app.listen(port);
console.log('Doctor appointment at ' + port);

I am calling router from server and executng application as node server.js

router.js

'use strict';
var express = require('express');
var doctorAppointment = require('./controllers/doctorAppointment');
module.exports = function() {
    var options = {
        caseSensitive: true
    };
    console.log("Router");
    // Instantiate an isolated express Router instance
    var router = express.Router(options);
    router.post('/appointment', doctorAppointment.takeAppoiment);
    return router;
}

I am calling controller method from router. Controller follows:

doctorAppointment.js

'use strict';

exports.takeAppoiment = function(req, res, next) {
    console.log("Inside appointment");
}

After execution, controller method is not calling

system@DT-LNX-315:~/Desktop/nodeJS/doctor-appointment$ node server.js
Router
Doctor appointment at 7777

And end point defined in http://localhost:7777/

coming as Cannot GET / in browser

And for http://localhost:7777/appointment

showing as Cannot GET /appointment. How to execute controller method?

2 Answers 2

2

please use

app.get('/appointment',function(req,res){
   doctorAppointment.takeAppoiment();
})

in router.js

Sign up to request clarification or add additional context in comments.

4 Comments

@zaynetro yes you can pass but i was just trying to give the idea.
Please tel where can I use post, get. If any more?
@Donthamsettivbhadrarao 'get' request means data you are sending to server will be sent in url which will be bad in case of sending sensitive data.
Doesn't mean necessarily that stuff will get sent in URL if you use GET. It might, if you use path variables or query parameters, but you can also send data in body, headers, etc. But it is kinda true that it's not as safe as POST though, that one is pretty easy to research/google.
0

You are handling POST requests to /appointment with this line of code:

router.post('/appointment', doctorAppointment.takeAppointment);

Change post to get if you need just to access the page, like so:

router.get('/appointment', doctorAppointment.takeAppointment);

Also you don't have a handler for the index page, that's why you are getting the error for / path. You can add the handler the similar way:

router.get('/', indexHandler);

1 Comment

Thanks for your answer. Cannot GET / in browser showing after execution. How to resolve this?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.