-1

I want to implement an API with POST method, But when i am starting my server then Error occured:

 3450:~/Desktop/koa/ctx$ node koa2.js 
    /home/358/Desktop/koa/ctx/koa2.js:9
    router.post('/locations', async (ctx, next) =>{ 
                                    ^
    SyntaxError: Unexpected token (
        at Object.exports.runInThisContext (vm.js:76:16)
        at Module._compile (module.js:542:28)
        at Object.Module._extensions..js (module.js:579:10)
        at Module.load (module.js:487:32)
        at tryModuleLoad (module.js:446:12)
        at Function.Module._load (module.js:438:3)
        at Module.runMain (module.js:604:10)
        at run (bootstrap_node.js:394:7)
        at startup (bootstrap_node.js:149:9)
        at bootstrap_node.js:509:3
    358@daffolap358-Latitude-3450:~/Desktop/koa/ctx$

Can anyone tell me where I am doing wrong ?

My code is : server.js:

var Koa =require('koa');
var middleware =require('koa-router');
var logger =require('koa-logger');
var parser =require('koa-bodyparser');
const router = middleware();
const app = new Koa();

router.post('/locations', async (ctx, next) =>{ 
    console.log("ctx");
    });
app
  .use(logger()) // Logs information.
  .use(parser()) // Parses json body requests.
  .use(router.routes()) // Assigns routes.
  .use(router.allowedMethods())
app.listen(5050, () => console.log('Listening on port 5050.'));
export default app;
Shubham Verma
  • 8,783
  • 6
  • 58
  • 79
  • 3
    This is the same as [your previous question](http://stackoverflow.com/questions/43063433/error-in-async-in-koa). Please close this one or the other. FWIW, your issue is that you don't use Node v7 or Babel, which are requirements to use `async/await`. – robertklep Mar 28 '17 at 08:03
  • Thank you so much. It works for me. – Shubham Verma Mar 28 '17 at 09:01
  • Hi@Mr. robertklep, Would you like to answer my this question: http://stackoverflow.com/questions/43061723/context-object-is-empty-while-receiving-post-request-in-koa ? – Shubham Verma Mar 28 '17 at 09:17

2 Answers2

1

Check your Node version. It should be >= 7.6.0. Otherwise koa-2 and async-await pattern will not work.

Rishabh
  • 185
  • 1
  • 13
1

You are getting this error because your node version is less than 7.6.0. It should be greater than 7.6 also Async/await support in Node 7.6 comes from updating V8, Chromium’s JavaScript engine, to version 5.5.

BHUVNESH KUMAR
  • 391
  • 4
  • 15