how can this socket.io javascript example be translated to typescript

95
January 23, 2020, at 4:00 PM

I'm testing the socket.io package and playing with the 'chat-example' (https://socket.io/get-started/chat/) and it works fine in javascript.

However, i would like to translate that example to TypeScript. 15 lines of code. Should be easy right? But no, I'm stuck with that.

if the file extension is .js, vscode's intellisense recognizes the 'require'd types, but as soon as I change the extension to .ts, the types are no longer recognized. I've tried to convert the 'require' statements to 'import' statements, but this also does not seem to work. What would be the proper typescript equivalent of the code below

var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var port = process.env.PORT || 3000;
app.get('/', function(req, res){
  res.sendFile(__dirname + '/index.html');
});
io.on('connection', function(socket){
  socket.on('chat message', function(msg){
    io.emit('chat message', msg);
  });
});
http.listen(port, function(){
  console.log('listening on *:' + port);
});
Answer 1

Have you tried import like below?

import express from 'express';
import http from 'http';
import io from 'socket.io';

Importe like this will not work

import { express } from 'express';
import { http } from 'http';
import { io } from 'socket.io';
READ ALSO
Reduce Array of Objects with Dynamic Keys

Reduce Array of Objects with Dynamic Keys

This seems like it should be pretty simple but my searching hasn't turned anything up

99
cant seem to remove visibility hidden from spans in a table

cant seem to remove visibility hidden from spans in a table

I'm trying to show the element behind the click after its been clicked,the span that holds the value has a hidden class, however upon click nothing happens, the relevant code:

76
require imports differently if array destructuring is used

require imports differently if array destructuring is used

I detected this error while producing a create-react-app

102
How to deploy express.js server to Netlify

How to deploy express.js server to Netlify

I am attempting to deploy a Vuejs, Node, Express, MongoDB (MEVN) stack application to Netlify

140