javascript - React Native 后台服务器启动报错

标签 javascript node.js react-native facebook-login backend

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

我遵循了一个教程。

教程链接 Here

server.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 start”时 cmd

最佳答案

在 npm 启动脚本中,您应该在开始处添加“node”:

"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"

关于javascript - React Native 后台服务器启动报错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48849122/

相关文章:

javascript - d3.js 散点图 - 缩放/拖动边界、缩放按钮、重置缩放、计算中位数

javascript - 如何使用map在react Native中过滤和添加来自json本地文件的数据的条件语句

ios - React Native Module 中的 EADemo 从未接收到委托(delegate)方法 handleEvent NSStreamEventOpenCompleted?

javascript - 单击按钮后关闭模式

typescript - 基于 Expo 将华为移动服务集成到 React Native 应用中

javascript - 从 Cypress Fixture 访问环境变量

javascript - 为每个元素的文本节点创建一个变量

javascript - 使用javascript计算数组中每个数字的出现次数

javascript - npm update 不更新某些包

node.js - 使用 selenium-webdriver/firefox (NodeJS) 设置 userAgent