Sending POST request to Vercel API page from USR-K2

599 Views Asked by At

I am trying some IoT stuff and i use USR-K2 to receive data and send them to website api where I proccess them. Website is hosted on Vercel.

The problem is I cant really read data on production. When i send the data to my localhost, it works properly.

I do have a file called readData.ts in directory "root/pages/api/"

import { NowRequest, NowResponse } from "@vercel/node";

export default (request: NowRequest, response: NowResponse) => {
  console.log("api called");
  const Data = request.body;

  console.log("data from api", Data);

  response.status(200).send(`Hello!`);
};

I also tried to configure http server in directory "root/server" according to NextJS Custom server example. My server index looks like this:

import { createServer } from "http";
import next from "next";
import { buffer } from "micro";

const port = parseInt(process.env.PORT || "3000", 10);
const dev = process.env.NODE_ENV !== "production";
const app = next({ dev });

app.prepare().then(() => {
  createServer(async (req) => {
    
    const buf = await buffer(req);
    console.log(buf);
    
  }).listen(port);

  console.log(
    `> Server listening at http://localhost:${port} as ${
      dev ? "development" : process.env.NODE_ENV
    }`
  );
});

Again on localhost everything works properly when i use nodemon. But when i build the app and try to send data to Vercel, nothing happens.

Printscreen of USR-K2 settings: USR-K2 settings

I also tried to set remote port number to 3000.

Here are my questions:

  1. What port should i set to send data to?
  2. Does Vercel support this kind of data handling?
  3. Could the problem be in K2 settings? Its work mode is set to Httpd client which should be the right one (others are UDP/TCP Client/Server). Can i use URL as remote server addres?
  4. Any other ideas how to run this on Vercel? Or should i use separate server to handle requests and process data?

Thanks in advance for your responses.

0

There are 0 best solutions below