代码之家  ›  专栏  ›  技术社区  ›  Sazzad Hossain

反应本机后端服务器启动错误

  •  1
  • Sazzad Hossain  · 技术社区  · 7 年前

    我想在我的应用程序中使用facebook和google登录选项。我使用节点。js后端服务器。但后端不运行。每次运行npm start时,它都会返回错误。

    我遵循了一个教程。

    教程链接 Here

    服务器代码。js文件

        const transformFacebookProfile = (profile) => ({
      name: profile.name,
      avatar: profile.picture.data.url,
    });
    
    // Transform Google profile into user object
    const transformGoogleProfile = (profile) => ({
      name: profile.displayName,
      avatar: profile.image.url,
    });
    
    // Register Facebook Passport strategy
    passport.use(new FacebookStrategy(facebook,
      // Gets called when user authorizes access to their profile
      async (accessToken, refreshToken, profile, done)
        // Return done callback and pass transformed user object
        => done(null, transformFacebookProfile(profile._json))
    ));
    
    // Register Google Passport strategy
    passport.use(new GoogleStrategy(google,
      async (accessToken, refreshToken, profile, done)
        => done(null, transformGoogleProfile(profile._json))
    ));
    
    // Serialize user into the sessions
    passport.serializeUser((user, done) => done(null, user));
    
    // Deserialize user from the sessions
    passport.deserializeUser((user, done) => done(null, user));
    
    // Initialize http server
    const app = express();
    
    // Initialize Passport
    app.use(passport.initialize());
    app.use(passport.session());
    
    // Set up Facebook auth routes
    app.get('/auth/facebook', passport.authenticate('facebook'));
    
    app.get('/auth/facebook/callback',
      passport.authenticate('facebook', { failureRedirect: '/auth/facebook' }),
      // Redirect user back to the mobile app using Linking with a custom protocol OAuthLogin
      (req, res) => res.redirect('OAuthLogin://login?user=' + JSON.stringify(req.user)));
    
    // Set up Google auth routes
    app.get('/auth/google', passport.authenticate('google', { scope: ['profile'] }));
    
    app.get('/auth/google/callback',
      passport.authenticate('google', { failureRedirect: '/auth/google' }),
      (req, res) => res.redirect('OAuthLogin://login?user=' + JSON.stringify(req.user)));
    
    // Launch the server on the port 3000
    const server = app.listen(3000, () => {
      const { address, port } = server.address();
      console.log(`Listening at http://${address}:${port}`);
    });

    当我运行“npm启动”时 cmd

    1 回复  |  直到 7 年前
        1
  •  1
  •   Shahzad Krishna    7 年前

    在npm开始脚本中,应在开始处添加“节点”:

    "start": "node node_modules/nodemon/bin/nodemon.js -- node_modules/babel-cli/bin/babel-node.js server.js"
    

    而不是:

    "start": "node_modules/nodemon/bin/nodemon.js -- node_modules/babel-cli/bin/babel-node.js server.js"