我想跟随史蒂芬格里德在Udemy的全栈反应课程。
我浪费了几天试图解决阻止我遵循他的教程的代理错误 - 它似乎没有任何Q& A支持。
在我的authRoutes.js文件中,我有:
const passport = require('passport');
module.exports = (app) => {
app.get(
'/auth/google',
passport.authenticate('google', {
scope: ['profile', 'email']
})
);
app.get(
'/auth/google/callback',
passport.authenticate('google'),
(req, res) => {
res.redirect('/dashboard')
}
);
app.get('/api/logout', (req, res) => {
req.logout();
res.redirect('/');
});
app.get('/api/current_user', (req, res) => {
res.send(req.user);
});
};
在我的passport.js文件中,我有:
const passport = require('passport');
const GoogleStrategy = require('passport-google-oauth20').Strategy;
const OrcidStrategy = require('passport-orcid');
const LinkedinStrategy = require('passport-linkedin');
const keys = require('../config/keys');
const mongoose = require('mongoose');
const User = mongoose.model('users');
passport.serializeUser((user, done) => {
done(null, user.id);
});
passport.deserializeUser((id, done) => {
User.findById(id)
.then(user => {
done(null, user);
});
});
passport.use(
new GoogleStrategy({
clientID: keys.googleClientID,
clientSecret: keys.googleClientSecret,
callbackURL: '/auth/google/callback',
proxy: true
},
async (accessToken, refreshToken, profile, done) => {
const existingUser = await User.findOne({ googleId: profile.id })
if(existingUser){
//User already exists
return done(null, existingUser);
}
// create a new user
const user = await new User({ googleId: profile.id}).save()
done(null, user);
Stephen建议在客户端的package.json中使用代理脚本,将本地主机3000重定向到本地主机5000,如下所示:
"proxy": {
"/auth/google": {
"target": "http://localhost:5000"
},
"api/*": {
"target": "http://localhost:5000"
}
},
当我尝试在开发中运行它时,我收到以下错误。
代理错误:无法代理请求/ auth / google / callback?code = 4 / xxx from localhost:3000 to http://localhost:5000(ECONNRESET)。
日志提供了以下信息:
Error
2017-08-21T22:34:14.618863+00:00 app[web.1]: at Strategy.OAuth2Strategy._createOAuthError (/app/node_modules/passport-oauth2/lib/strategy.js:376:16)
2017-08-21T22:34:14.618866+00:00 app[web.1]: at IncomingMessage.<anonymous> (/app/node_modules/oauth/lib/oauth2.js:157:7)
2017-08-21T22:34:14.618864+00:00 app[web.1]: at /app/node_modules/passport-oauth2/lib/strategy.js:166:45
2017-08-21T22:34:14.618867+00:00 app[web.1]: at emitNone (events.js:110:20)
2017-08-21T22:34:14.618865+00:00 app[web.1]: at passBackControl (/app/node_modules/oauth/lib/oauth2.js:132:9)
2017-08-21T22:34:14.618868+00:00 app[web.1]: at IncomingMessage.emit (events.js:207:7)
2017-08-21T22:34:14.618869+00:00 app[web.1]: at endReadableNT (_stream_readable.js:1047:12)
2017-08-21T22:34:14.618869+00:00 app[web.1]: at _combinedTickCallback (internal/process/next_tick.js:102:11)
2017-08-21T22:34:14.618870+00:00 app[web.1]: at process._tickCallback (internal/process/next_tick.js:161:9)
When I try to push to heroku and test it in production, I get a response saying cannot get /".
heroku logs
2017-08-21T18:07:07.553330+00:00 app[web.1]: > server@1.0.0 start /app
2017-08-21T18:07:07.553331+00:00 app[web.1]: > node index.js
2017-08-21T18:07:07.553332+00:00 app[web.1]:
2017-08-21T18:07:07.678891+00:00 app[web.1]: module.js:487
2017-08-21T18:07:07.678894+00:00 app[web.1]: throw err;
2017-08-21T18:07:07.678894+00:00 app[web.1]: ^
2017-08-21T18:07:07.678895+00:00 app[web.1]:
2017-08-21T18:07:07.678896+00:00 app[web.1]: Error: Cannot find module 'express'
2017-08-21T18:07:07.678897+00:00 app[web.1]: at Function.Module._load (module.js:437:25)
2017-08-21T18:07:07.678897+00:00 app[web.1]: at Function.Module._resolveFilename (module.js:485:15)
2017-08-21T18:07:07.678898+00:00 app[web.1]: at Module.require (module.js:513:17)
2017-08-21T18:07:07.678899+00:00 app[web.1]: at Object.<anonymous> (/app/index.js:1:79)
2017-08-21T18:07:07.678900+00:00 app[web.1]: at Object.Module._extensions..js (module.js:580:10)
2017-08-21T18:07:07.678900+00:00 app[web.1]: at Module._compile (module.js:569:30)
2017-08-21T18:07:07.678899+00:00 app[web.1]: at require (internal/module.js:11:18)
2017-08-21T18:07:07.678901+00:00 app[web.1]: at Module.load (module.js:503:32)
2017-08-21T18:07:07.678901+00:00 app[web.1]: at tryModuleLoad (module.js:466:12)
2017-08-21T18:07:07.678902+00:00 app[web.1]: at Function.Module._load (module.js:458:3)
2017-08-21T18:07:07.708657+00:00 app[web.1]: npm ERR! code ELIFECYCLE
2017-08-21T18:07:07.709012+00:00 app[web.1]: npm ERR! errno 1
2017-08-21T18:07:07.709236+00:00 app[web.1]: npm ERR! server@1.0.0 start: `node index.js`
2017-08-21T18:07:07.709696+00:00 app[web.1]: npm ERR!
2017-08-21T18:07:07.709442+00:00 app[web.1]: npm ERR! Exit status 1
2017-08-21T18:07:07.710116+00:00 app[web.1]: npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
2017-08-21T18:07:07.709913+00:00 app[web.1]: npm ERR! Failed at the server@1.0.0 start script.
2017-08-21T18:07:07.711990+00:00 app[web.1]:
2017-08-21T18:07:07.712301+00:00 app[web.1]: npm ERR! A complete log of this run can be found in:
2017-08-21T18:07:07.712514+00:00 app[web.1]: npm ERR! /app/.npm/_logs/2017-08-21T18_07_07_696Z-debug.log
2017-08-21T18:07:07.826361+00:00 heroku[web.1]: State changed from starting to crashed
2017-08-21T18:07:07.810373+00:00 heroku[web.1]: Process exited with status 1
2017-08-21T22:33:31.000000+00:00 app[api]: Build started by user himl@gmail.com
2017-08-21T22:33:49.588841+00:00 heroku[web.1]: State changed from crashed to starting
2017-08-21T22:33:48.364576+00:00 app[api]: Deploy c9f339cc by user himl@gmail.com
2017-08-21T22:33:48.364576+00:00 app[api]: Release v13 created by user himl@gmail.com
2017-08-21T22:33:31.000000+00:00 app[api]: Build succeeded
2017-08-21T22:33:53.125398+00:00 heroku[web.1]: Starting process with command `npm start`
2017-08-21T22:33:58.507062+00:00 app[web.1]: > server@1.0.0 start /app
2017-08-21T22:33:58.507041+00:00 app[web.1]:
2017-08-21T22:33:58.507063+00:00 app[web.1]: > node index.js
2017-08-21T22:33:58.507063+00:00 app[web.1]:
2017-08-21T22:34:00.085012+00:00 app[web.1]: Db.prototype.authenticate method will no longer be available in the next major release 3.x as MongoDB 3.6 will only allow auth against users in the admin db and will no longer allow multiple credentials on a socket. Please authenticate using MongoClient.connect with auth credentials.
2017-08-21T22:34:00.051388+00:00 app[web.1]: (node:17) DeprecationWarning: `open()` is deprecated in mongoose >= 4.11.0, use `openUri()` instead, or set the `useMongoClient` option if using `connect()` or `createConnection()`. See http://mongoosejs.com/docs/connections.html#use-mongo-client
2017-08-21T22:34:00.250219+00:00 heroku[web.1]: State changed from starting to up
2017-08-21T22:34:01.338805+00:00 app[web.1]: (node:17) DeprecationWarning: Mongoose: mpromise (mongoose's default promise library) is deprecated, plug in your own promise library instead: http://mongoosejs.com/docs/promises.html
2017-08-21T22:34:01.404941+00:00 heroku[router]: at=info method=GET path="/" host=xxx.herokuapp.com request_id=xxx fwd="202.191.1.40" dyno=web.1 connect=2ms service=159ms status=404 bytes=383 protocol=https
2017-08-21T22:34:09.819224+00:00 heroku[router]: at=info method=GET path="/auth/google/callback?code=xxx" host=xxx.herokuapp.com request_id=xxx fwd="202.191.1.40" dyno=web.1 connect=1ms service=296ms status=500 bytes=404 protocol=https
2017-08-21T22:34:09.818012+00:00 app[web.1]: Error
2017-08-21T22:34:09.818030+00:00 app[web.1]: at /app/node_modules/passport-oauth2/lib/strategy.js:166:45
2017-08-21T22:34:09.818027+00:00 app[web.1]: at Strategy.OAuth2Strategy.parseErrorResponse (/app/node_modules/passport-oauth2/lib/strategy.js:329:12)
2017-08-21T22:34:09.818028+00:00 app[web.1]: at Strategy.OAuth2Strategy._createOAuthError (/app/node_modules/passport-oauth2/lib/strategy.js:376:16)
2017-08-21T22:34:09.818031+00:00 app[web.1]: at /app/node_modules/oauth/lib/oauth2.js:191:18
2017-08-21T22:34:09.818032+00:00 app[web.1]: at passBackControl (/app/node_modules/oauth/lib/oauth2.js:132:9)
2017-08-21T22:34:09.818033+00:00 app[web.1]: at IncomingMessage.<anonymous> (/app/node_modules/oauth/lib/oauth2.js:157:7)
2017-08-21T22:34:09.818034+00:00 app[web.1]: at emitNone (events.js:110:20)
2017-08-21T22:34:09.818035+00:00 app[web.1]: at IncomingMessage.emit (events.js:207:7)
2017-08-21T22:34:09.818035+00:00 app[web.1]: at endReadableNT (_stream_readable.js:1047:12)
2017-08-21T22:34:09.818036+00:00 app[web.1]: at _combinedTickCallback (internal/process/next_tick.js:102:11)
2017-08-21T22:34:09.818036+00:00 app[web.1]: at process._tickCallback (internal/process/next_tick.js:161:9)
2017-08-21T22:34:14.620378+00:00 heroku[router]: at=info method=GET path="/auth/google/callback?code=xxx" host=xxx.herokuapp.com request_id=xxx fwd="202.191.1.40" dyno=web.1 connect=1ms service=84ms status=500 bytes=404 protocol=https
2017-08-21T22:34:14.665428+00:00 heroku[router]: at=info method=GET path="/auth/google" host=xxx.herokuapp.com request_id=xxx fwd="202.191.1.40" dyno=web.1 connect=1ms service=15ms status=302 bytes=390 protocol=https
2017-08-21T22:34:14.618862+00:00 app[web.1]: at Strategy.OAuth2Strategy.parseErrorResponse (/app/node_modules/passport-oauth2/lib/strategy.js:329:12)
2017-08-21T22:34:14.618864+00:00 app[web.1]: at /app/node_modules/oauth/lib/oauth2.js:191:18
2017-08-21T22:34:14.618849+00:00 app[web.1]: Error
2017-08-21T22:34:14.618863+00:00 app[web.1]: at Strategy.OAuth2Strategy._createOAuthError (/app/node_modules/passport-oauth2/lib/strategy.js:376:16)
2017-08-21T22:34:14.618866+00:00 app[web.1]: at IncomingMessage.<anonymous> (/app/node_modules/oauth/lib/oauth2.js:157:7)
2017-08-21T22:34:14.618864+00:00 app[web.1]: at /app/node_modules/passport-oauth2/lib/strategy.js:166:45
2017-08-21T22:34:14.618867+00:00 app[web.1]: at emitNone (events.js:110:20)
2017-08-21T22:34:14.618865+00:00 app[web.1]: at passBackControl (/app/node_modules/oauth/lib/oauth2.js:132:9)
2017-08-21T22:34:14.618868+00:00 app[web.1]: at IncomingMessage.emit (events.js:207:7)
2017-08-21T22:34:14.618869+00:00 app[web.1]: at endReadableNT (_stream_readable.js:1047:12)
2017-08-21T22:34:14.618869+00:00 app[web.1]: at _combinedTickCallback (internal/process/next_tick.js:102:11)
2017-08-21T22:34:14.618870+00:00 app[web.1]: at process._tickCallback (internal/process/next_tick.js:161:9)
2017-08-21T22:34:16.104098+00:00 heroku[router]: at=info method=GET path="/auth/google/callback?code=xxx" host=xxx.herokuapp.com request_id=dc8df15c-ffe0-4b58-a4aa-8e189c593b5a fwd="202.191.1.40" dyno=web.1 connect=3ms service=656ms status=404 bytes=403 protocol=https
2017-08-21T22:34:27.742845+00:00 heroku[router]: at=info method=GET path="/auth/google" host=xxx.herokuapp.com request_id=xxx fwd="202.191.1.40" dyno=web.1 connect=1ms service=42ms status=302 bytes=390 protocol=https
2017-08-21T22:34:28.804666+00:00 heroku[router]: at=info method=GET path="/auth/google/callback?code=xxx" host=xxx.herokuapp.com request_id=xxx fwd="202.191.1.40" dyno=web.1 connect=1ms service=464ms status=404 bytes=403 protocol=https
2017-08-21T22:34:29.557037+00:00 heroku[router]: at=info method=GET path="/auth/google" host=xxx.herokuapp.com request_id=xxx fwd="202.191.1.40" dyno=web.1 connect=1ms service=8ms status=302 bytes=390 protocol=https
2017-08-21T22:34:30.642087+00:00 heroku[router]: at=info method=GET path="/auth/google/callback?code=xxx" host=enigmatic-brook-78689.herokuapp.com request_id=xxx fwd="202.191.1.40" dyno=web.1 connect=1ms service=536ms status=404 bytes=403 protocol=https
2017-08-21T22:34:39.916993+00:00 heroku[router]: at=info method=GET path="/" host=xxx.herokuapp.com request_id=xxx fwd="202.191.1.40" dyno=web.1 connect=1ms service=16ms status=404 bytes=383 protocol=https
2017-08-21T22:34:41.023527+00:00 heroku[router]: at=info method=GET path="/api/current_user" host=xxx.herokuapp.com request_id=xxx fwd="202.191.1.40" dyno=web.1 connect=2ms service=18ms status=304 bytes=150 protocol=https
2017-08-21T22:34:40.958563+00:00 heroku[router]: at=info method=GET path="/api/current_user" host=xxx.herokuapp.com request_id=xxx fwd="202.191.1.40" dyno=web.1 connect=1ms service=20ms status=304 bytes=150 protocol=https
2017-08-21T23:10:03.305689+00:00 heroku[web.1]: Idling
2017-08-21T23:10:03.306415+00:00 heroku[web.1]: State changed from up to down
2017-08-21T23:10:04.202040+00:00 heroku[web.1]: Stopping all processes with SIGTERM
2017-08-21T23:10:04.352701+00:00 heroku[web.1]: Process exited with status 143
2017-08-23T03:28:49.000000+00:00 app[api]: Build started by user himl@gmail.com
2017-08-23T03:29:01.748781+00:00 app[api]: Release v14 created by user himl@gmail.com
2017-08-23T03:29:01.748781+00:00 app[api]: Deploy 6fa5bda7 by user himl@gmail.com
2017-08-23T03:29:02.060735+00:00 heroku[web.1]: State changed from down to starting
2017-08-23T03:28:49.000000+00:00 app[api]: Build succeeded
2017-08-23T03:29:04.226461+00:00 heroku[web.1]: Starting process with command `npm start`
2017-08-23T03:29:06.893145+00:00 app[web.1]:
2017-08-23T03:29:06.893164+00:00 app[web.1]: > server@1.0.0 start /app
2017-08-23T03:29:06.893165+00:00 app[web.1]: > node index.js
2017-08-23T03:29:06.893166+00:00 app[web.1]:
2017-08-23T03:29:07.397451+00:00 app[web.1]: (node:17) DeprecationWarning: `open()` is deprecated in mongoose >= 4.11.0, use `openUri()` instead, or set the `useMongoClient` option if using `connect()` or `createConnection()`. See http://mongoosejs.com/docs/connections.html#use-mongo-client
2017-08-23T03:29:07.422226+00:00 app[web.1]: Db.prototype.authenticate method will no longer be available in the next major release 3.x as MongoDB 3.6 will only allow auth against users in the admin db and will no longer allow multiple credentials on a socket. Please authenticate using MongoClient.connect with auth credentials.
2017-08-23T03:29:08.014335+00:00 heroku[web.1]: State changed from starting to up
2017-08-23T03:29:33.093123+00:00 heroku[router]: at=info method=GET path="/" host=xxx.herokuapp.com request_id=xxx fwd="202.191.1.40" dyno=web.1 connect=0ms service=41ms status=404 bytes=383 protocol=https
2017-08-23T03:29:33.078119+00:00 app[web.1]: (node:17) DeprecationWarning: Mongoose: mpromise (mongoose's default promise library) is deprecated, plug in your own promise library instead: http://mongoosejs.com/docs/promises.html
2017-08-23T03:30:06.960381+00:00 heroku[router]: at=info method=GET path="/" host=xxx.herokuapp.com request_id=xxx fwd="202.191.1.40" dyno=web.1 connect=0ms service=15ms status=404 bytes=383 protocol=https
2017-08-23T03:30:07.490909+00:00 heroku[router]: at=info method=GET path="/" host=xxx.herokuapp.com request_id=xxx fwd="202.191.1.40" dyno=web.1 connect=1ms service=5ms status=404 bytes=383 protocol=https
有谁知道如何使用Stephen的课程方法来设置基本的mern堆栈应用程序?
有没有人在克服这些具体问题上取得任何成功?这是浪费时间发布到斯蒂芬的Q&amp; A - 他没有回答问题。
答案 0 :(得分:0)
此问题是由将版本从1.1.15迁移到2.0.1引起的。 要解决此问题,您现在应该在package.json文件中删除代理设置,并改为执行以下操作:
在您的客户文件夹中:
$ npm install http-proxy-middleware --save
$ # or
$ yarn add http-proxy-middleware
像这样将条目放入src / setupProxy.js:
const proxy = require('http-proxy-middleware')
module.exports = function(app) {
app.use(proxy('/api', { target: 'http://localhost:5000/' }))
app.use(proxy('/*.svg', { target: 'http://localhost:5000/' }))
}
注意:您不必导入setupProxy.js。
有关更多信息,请访问以下链接:https://github.com/facebook/create-react-app/issues/5103
答案 1 :(得分:0)
const { createProxyMiddleware }= require('http-proxy-middleware');
module.exports = function(app) {
app.use( createProxyMiddleware('/auth/google', { target: 'http://localhost:5000' }));
app.use( createProxyMiddleware('/api/**', { target: 'http://localhost:5000' }));
};