将nodejs服务器转换为ES6语法

时间:2018-03-02 21:37:25

标签: javascript node.js ecmascript-6

我的nodejs服务器是这样的:

const app  = require('express')();
const http = require('http').Server(app);
const io   = require('socket.io')(http);

const port = process.env.PORT || 8000;

// Routes ===============================================
require('./routes.js')(app);

// Socket.io ============================================
require('./socket.js')(io);

// Launch ===============================================
http.listen(port, function() {
    console.log('The magic happens on port '+port);
});

我想将其转换为ES6语法,但我不知道如何尝试这样的事情:

import express  = 'express';
import http = 'http';
import io = 'socket.io';
import routes './routes';
import sockets './sockets';

const port = process.env.PORT || 8000;

// Routes ===============================================
routes(express);

// Socket.io ============================================
sockets(io);

// Launch ===============================================
http.listen(port, function() {
    console.log('The magic happens on port '+port);
});

但它没有用。

1 个答案:

答案 0 :(得分:1)

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

import routes from './routes';
import sockets from './sockets';

const app = express();
const server = http.Server(app);
const io = ioClient(server);

app.use(routes);
const mySockets = sockets(io);