javascript - what's the different between app.post and app.use in nodes express? -


i use command curl -h "content-type: application/json" -d '{"name":"sparc_core","port":["p1", "p2"]}' http://127.0.0.1:3000/add_module test nodejs server.

at first, code follows:

app.post('/add_module', bodyparser.json());  app.post('/add_module', bodyparser.urlencoded()); app.post('/add_module', function(req, res, next) {     req.body = json.parse(req.body.data);     next(); }); app.post('/add_module', function(req, res) {     console.log("start submitting");     console.log(req.body); ... ... 

after run curl command, nodes server output error information below:

syntaxerror: unexpected token u
@ object.parse (native)
@ object.app.post.res.send.error [as handle] (/home/xtec/documents/xtec- simict/sim/app.js:80:21)
@ next_layer (/home/xtec/documents/xtec- simict/sim/node_modules/express/lib/router/route.js:103:13)
@ route.dispatch (/home/xtec/documents/xtec-simict/sim/node_modules/express/lib/router/route.js:107:5)
@ /home/xtec/documents/xtec- simict/sim/node_modules/express/lib/router/index.js:205:24
@ function.proto.process_params (/home/xtec/documents/xtec-simict/sim/node_modules/express/lib/router/index.js:269:12)
@ next (/home/xtec/documents/xtec-simict/sim/node_modules/express/lib/router/index.js:199:19)
@ next_layer (/home/xtec/documents/xtec-simict/sim/node_modules/express/lib/router/route.js:77:14)
@ object.urlencodedparser [as handle] (/home/xtec/documents/xtec-simict/sim/node_modules/body-parser/index.js:67:27)
@ next_layer (/home/xtec/documents/xtec-simict/sim/node_modules/express/lib/router/route.js:103:13)
post /add_module 500 7ms - 1021b

then, modify code follows:

app.use(bodyparser.json()); app.use(bodyparser.urlencoded({     extended: true }));  app.post('/add_module', function(req, res) {     console.log("start submitting");     console.log(req.body); ... ... 

i run same curl command , works ok!

so want know differents between app.use , app.post. need help, thank much.

app.use() use include middleware/interceptor function executed before actual function executed when api call .

for more details refer - express offcial website

for example :

app.use(cors());     app.post("/",function(req,res){     }); 

the above line of code equivalent

app.post("/",cors(),function(req,res){ }); 

app.post , app.get , app.put , app.delete define http method api .
please refer link http://www.tutorialspoint.com/http/http_methods.htm more details http methods

in case

app.use(bodyparser.json()); app.use(bodyparser.urlencoded({     extended: true }));  app.post('/add_module', function(req, res) {     console.log("start submitting");     console.log(req.body); } 

when /add_module api called , first bodyparser.json() bodyparser.urlencoded({ extended: true }) function called after that

 function(req, res) {         console.log("start submitting");         console.log(req.body);}  

is called . bodyparser.json() , bodyparse.urlencoded({extended:true}) required body object request in called function(req,res)


Comments

  1. @admin
    how javascript work and app.post and app.use list detected
    Regards,
    Deepika Verma

    ReplyDelete

Post a Comment

Popular posts from this blog

java - Plugin org.apache.maven.plugins:maven-install-plugin:2.4 or one of its dependencies could not be resolved -

Round ImageView Android -

How can I utilize Yahoo Weather API in android -