Socket.IO无法从前端发送数据

时间:2018-02-22 17:12:40

标签: node.js express post socket.io

我相信我在从前端发送数据时遇到问题,而服务器无法发布请求。我的服务器正在使用快递。当dev工具在浏览器中打开时,网络出现404错误,无法POST /socket.io/。我过去两天一直在研究这个问题而且无法得出结论,只是让我知道提供什么代码并发布它。

server.js

require('dotenv').config();
const bodyParser = require('body-parser')
, express = require('express')
, cors = require('cors')
, massive = require('massive')
, passport = require('passport')
, session = require('express-session')
, Auth0Strategy = require('passport-auth0');
const socketIO = require('socket.io');


const port = process.env.SERVER_PORT;
const app = express()
    , io = socketIO(massive(process.env.CONNECTION_STRING).then(db => {
        app.set('db', db)
        app.listen(port, console.log(`this server is running on port ${port}.`))
    }))
    app.use("/js", express.static(__dirname + "/client/js"));
app.use(bodyParser.json());
app.use(cors());

io.on('connection', socket => {
    console.log('User Connected');
    socket.emit("welcome", {userID: socket.id})
  
    socket.on('message sent', function(data) {
      console.log(data)
      data.user = this.id
      io.emit('message dispatched', data);
    })
  

    socket.on('message sent', function(data) {
      data.user = this.id
      console.log(data)
      socket.broadcast.emit('message dispatched', data);
    })
  
    socket.on('disconnect', () => {
      console.log('User Disconnected');
    })
  });

// ---------SESSIONS--------
app.use(session({
    secret: process.env.SESSION_SECRET,
    resave: false,
    saveUninitialized: true
}))
app.use(passport.initialize());
app.use(passport.session());

// -------------AUTH0------------
passport.use(new Auth0Strategy({
    domain: process.env.AUTH_DOMAIN,
    clientID: process.env.AUTH_CLIENT_ID,
    clientSecret: process.env.AUTH_CLIENT_SECRET,
    callbackURL: process.env.AUTH_CALLBACK_URL,
    scope: 'openid profile'
}, function(accessToken, refresjToken, extraParams, profile, done){
    let {_json} = profile;

    let {displayName, user_id, picture} = profile;
    const db = app.get('db')

    db.find_user(user_id).then(function(user) {
        console.log(user)
        if (!user[0]) {
            db.create_user([
                displayName,
                'test@email.com',
                picture,
                user_id
            ]).then(user => {
                return done(null, user[0].id)
            })
        } else {
            return done(null, user[0].id)
        }
    })

}))

passport.serializeUser((id, done) => {
    done(null, id);
})
passport.deserializeUser((id, done) => {
    app.get('db').find_session_user(id).then(function(user) {
        return done(null,user[0])
    })
})

// -------AUTH0 END POINTS-------
app.get('/auth', passport.authenticate('auth0'));
app.get('/auth/callback', passport.authenticate('auth0', {
    successRedirect: 'http://localhost:3001/#/login',
    failureRedirect: 'http://localhost:3001/'
}));
app.get('/auth/me', (req,res) => {
    if (!req.user) {
        res.status(404).send('user not found');
    } else {
        res.status(200).send(req.user);
    }
})

app.get('/auth/logout', function(req,res) {
    req.logOut();
    res.redirect('http://localhost:3000/')
})

0 个答案:

没有答案