Thursday, 11 May 2017

Micro.js transpilation with babel.js

I try transpilate the pictures.js with babel.js and every time I run the script micro -p 5000 pictures.js, the output I get from NPM is the following:

micro: Error when importing D:\PLATZI\CARRERA DE BACKEND PROFESIONAL\8-CURSO PROFESIONAL DE NODE Y JAVASCRIPT\platzigram-api\pictures.js: D:\PLATZI\CARRERA DE BACKEND PROFESIONAL\8-CURSO PROFESIONAL DE NODE Y JAVASCRIPT\platzigram-api\pictures.js:3
import { send, json } from 'micro'
^^^^^^
SyntaxError: Unexpected token import
    at createScript (vm.js:53:10)
    at Object.runInThisContext (vm.js:95:10)
    at Module._compile (module.js:543:28)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:488:32)
    at tryModuleLoad (module.js:447:12)
    at Function.Module._load (module.js:439:3)
    at Module.require (module.js:498:17)
    at require (internal/module.js:20:19)
    at module.exports.file (C:\Users\JuanDiego\AppData\Roaming\npm\node_modules\micro\lib\module.js:6:11)
    at module.exports (C:\Users\JuanDiego\AppData\Roaming\npm\node_modules\micro\lib\index.js:9:47)
    at Object.<anonymous> (C:\Users\JuanDiego\AppData\Roaming\npm\node_modules\micro\bin\micro.js:90:18)
    at Module._compile (module.js:571:32)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:488:32)
    at tryModuleLoad (module.js:447:12)

pictures.js

'use strict'

import { send, json } from 'micro'
import HttpHash from 'http-hash'
import Db from 'platzigram-db'
import config from './config'
import utils from './lib/utils'
import DbStub from './test/stub/db'

const env = process.env.NODE_ENV || 'production'
let db = new Db(config.db)

if (env === 'test') {
  db = new DbStub()
}

const hash = HttpHash()

hash.set('GET /tag/:tag', async function bytag (req, res, params) {
  let tag = params.tag
  await db.connect()
  let images = await db.getImagesByTag(tag)
  await db.disconnect()
  send(res, 200, images)
})

hash.set('GET /list', async function list (req, res, params) {
  await db.connect()
  let images = await db.getImages()
  await db.disconnect()
  send(res, 200, images)
})

hash.set('GET /:id', async function getPicture (req, res, params) {
  let id = params.id
  await db.connect()
  let image = await db.getImage(id)
  await db.disconnect()
  send(res, 200, image)
})

hash.set('POST /', async function postPicture (req, res, params) {
  let image = await json(req)

  try {
    let token = await utils.extractToken(req)
    let encoded = await utils.verifyToken(token, config.secret)
    if (encoded && encoded.userId !== image.userId) {
      throw new Error('invalid token')
    }
  } catch (e) {
    return send(res, 401, { error: 'invalid token' })
  }

  await db.connect()
  let created = await db.saveImage(image) // await para resolver la promesa en el stub/db.js
  await db.disconnect()
  send(res, 201, created)
})

hash.set('POST /:id/like', async function likePicture (req, res, params) {
  let id = params.id
  await db.connect()
  let image = await db.likeImage(id)
  await db.disconnect()
  send(res, 200, image)
})

export default async function main (req, res) {
  let { method, url } = req
  let match = hash.get(`${method.toUpperCase()} ${url}`)

  if (match.handler) {
    try {
      await match.handler(req, res, match.params)
    } catch (e) {
      send(res, 500, { error: e.message })
    }
  } else {
    send(res, 404, { error: 'route not found' })
  }
}

package.js

{
  "name": "platzigram-api",
  "version": "1.0.0",
  "description": "Platzigram REST API",
  "scripts": {
    "lint": "standard",
    "test": "npm run lint && ava test/**-test.js"
  },
  "standard": {
    "parser": "babel-eslint"
  },
  "ava": {
    "require": [
      "babel-register"
    ]
  },
  "babel": {
    "presets": [
      "es2015"
    ],
    "plugins": [
      "transform-runtime",
      "transform-async-to-generator"
    ]
  },
  "repository": {
    "type": "git"
  },
  "author": "Juan Diego Silva",
  "license": "MIT",
  "bugs": {
  },
  "devDependencies": {
    "ava": "^0.19.1",
    "babel-eslint": "^7.2.3",
    "babel-register": "^6.24.1",
    "standard": "^10.0.2",
    "test-listen": "^1.0.2"
  },
  "dependencies": {
    "babel-plugin-transform-async-to-generator": "^6.24.1",
    "babel-plugin-transform-runtime": "^6.23.0",
    "babel-preset-es2015": "^6.24.1",
    "babel-runtime": "^6.23.0",
    "http-hash": "^2.0.0",
    "jsonwebtoken": "^7.4.0",
    "micro": "^7.3.2",
    "request": "^2.81.0",
    "request-promise": "^4.2.0",
    "token-extractor": "^0.1.6",
    "uuid-base62": "^0.1.0"
  }
}

any idea how transpilar the pictures.js with babel?



via Juan Diego Silva

No comments:

Post a Comment