0

Rather than using request, to handle the code from the browser side, I have opt for import. Now the following line is buggy, I get from it Unexpected token '('

import io from "socket.io"(server, {something});

Here is more Code to compare.

import express from 'express';
import http from 'http';
  
var app = express();
var server = http.createServer(app);

import io from "socket.io"(server, {
  cors: {
    origin: "http://example.com",
    methods: ["GET", "POST"],
    allowedHeaders: ["my-custom-header"],
    credentials: true
  }
});
Philipp
  • 23
  • 1
  • 8
  • Does this answer your question? [Node.js - SyntaxError: Unexpected token import](https://stackoverflow.com/questions/39436322/node-js-syntaxerror-unexpected-token-import) – yeya Mar 07 '21 at 12:36
  • unfortunately it does not, because my unexpected Token ist not "import", but simply the "(" the bracket after "socket.io" – Philipp Mar 07 '21 at 12:41

1 Answers1

0

imports are static, you can't write it this way.

Just see the official docs, there are examples with import

https://socket.io/docs/v3/server-initialization/index.html

import { createServer } from "http";
import { Server } from "socket.io";

const httpServer = createServer();
const io = new Server(httpServer, {
  // ...
});

io.on("connection", (socket) => {
  // ...
});

httpServer.listen(3000);
yeya
  • 1,968
  • 1
  • 21
  • 31