Skip to content

🐛 Error thrown when using exposed server function with HTTP POST method #332

@edysegura

Description

@edysegura

When attempting to use the exposed server function ExposedServerFunctions.fetchData() with an HTTP POST method, an error is thrown at line 18 of the deserialize.js file, which can be found in the nullstack/shared folder. The error message reads: "SyntaxError: Unexpected token o in JSON at position 1". This occurs when the function attempts to parse JSON data from the request.body, but encounters an unexpected token.

This issue can be reproduced by running this sample project and sending a POST request to the http://localhost:3000/test-post endpoint. A footage video of the issue can be found at this Loom link.

Sample code server.js

import Nullstack from 'nullstack'

import express from 'express'

import Application from './src/Application'
import ExposedServerFunctions from './src/ExposedServerFunctions'

const context = Nullstack.start(Application)
context.server.use(express.json())
context.server.post('/test-post', ExposedServerFunctions.fetchData)

context.start = async function start() {
  // https://nullstack.app/application-startup
}

export default context

Sample code ExposedServerFunctions.jsx

import Nullstack from 'nullstack'

class ExposedServerFunctions extends Nullstack {

  static async fetchData({ request }) {
    console.log(`👀 Yeah! Im here! url: ${request.url}`)
    console.log('request.body', request.body)
    return {
      status: "🐴 xurupita's farm 🐴",
      body: request.body,
    }
  }

}

export default ExposedServerFunctions

Expection thrown

SyntaxError: Unexpected token o in JSON at position 1
    at JSON.parse (<anonymous>)
    at null.deserialize (webpack://nullstack-testy/node_modules/nullstack/shared/deserialize.js:18:1)
    at null.<anonymous> (webpack://nullstack-testy/node_modules/nullstack/server/server.js:52:1)
    at Layer.handle (webpack://nullstack-testy/node_modules/express/lib/router/layer.js:95:1)
    at null.next (webpack://nullstack-testy/node_modules/express/lib/router/route.js:144:1)
    at null.textParser (webpack://nullstack-testy/node_modules/body-parser/lib/types/text.js:60:1)
    at Layer.handle (webpack://nullstack-testy/node_modules/express/lib/router/layer.js:95:1)
    at null.next (webpack://nullstack-testy/node_modules/express/lib/router/route.js:144:1)
    at Route.dispatch (webpack://nullstack-testy/node_modules/express/lib/router/route.js:114:1)
    at Layer.handle (webpack://nullstack-testy/node_modules/express/lib/router/layer.js:95:1)

Issue caught on video

Sample repo

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions