该代码可在本地计算机上正常运行,但在Heroku上部署时不会连接到后端

时间:2019-01-30 15:50:12

标签: reactjs mongodb express heroku create-react-app

连接数据库时- 错误消息:无法加载资源:net :: ERR_CONNECTION_REFUSED localhost:3001 / api / users / login

我已经使用create-react-app和express服务器创建了一个react应用。我已经在Heroku上部署了这两者,但是它没有连接到后端。

我的server.js代码是-

const express=require('express');
const mongoose=require('mongoose');
const passport=require('passport');
const bodyparser=require('body-parser');
const path=require('path');

const cors=require('cors');
const users=require('./routes/api/users')
const posts=require('./routes/api/posts')
const profile=require('./routes/api/profile')

const app=express();
const db=require('./config/keys.js').mongoURI;
mongoose.connect(process.env.MONGODB_URI||db,{useNewUrlParser:true})
    .then(()=>console.log("MONGODB connected"))
    .catch(err=>console.log(err))


app.use(bodyparser.urlencoded({extended:false}));
app.use(bodyparser.json());
app.use(passport.initialize());

require('./config/passport')(passport);
//use routes
app.use(cors())

app.use('/api/users',users)
app.use('/api/profile',profile)
app.use('/api/posts',posts)

if(process.env.NODE_ENV==='production')
{
app.use(express.static(path.join(__dirname, 'client/build')))
// Anything that doesn't match the above, send back index.html
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname + '/client/build/index.html'))
})
}


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

app.listen(port,()=>console.log(`server running on port ${port}`));

我的客户端package.json是-

 {

 "name": "client",

 "version": "0.1.0",

 "proxy": "http://localhost:3001",

 "private": true,

 "scripts": {
  "start": "node scripts/start.js",
  "build": "node scripts/build.js",
  "test": "node scripts/test.js"
  },
  //other code
 }

我的服务器端package.json是-

{
    "name": "devsocial",
    "version": "1.0.0",
    "description": "social network for developers",
    "main": "server.js",
    "scripts": {
        "client-install": "npm install --prefix client",
        "start": "node server.js",
        "server": "nodemon server.js",
        "client": "npm start --prefix client",
        "dev": "concurrently \"npm run server\" \"npm run client\"",
        "heroku-postbuild": "NPM_CONFIG_PRODUCTION=false npm install --prefix client && npm run build --prefix client"
    },
    "author": "anuj shrivastav",
    "license": "ISC",
    "devDependencies": {
        "nodemon": "^1.18.9"
    }
}

heroku日志-

2019-01-30T15:03:28.000000+00:00 app[api]: Build started by user anujshrivastav97@gmail.com
2019-01-30T15:05:16.939873+00:00 heroku[web.1]: Restarting
2019-01-30T15:05:17.668208+00:00 heroku[web.1]: Stopping all processes with SIGTERM
2019-01-30T15:05:16.755468+00:00 app[api]: Deploy eeb928ff by user anujshrivastav97@gmail.com
2019-01-30T15:05:17.748326+00:00 heroku[web.1]: Process exited with status 143
2019-01-30T15:05:18.000000+00:00 app[api]: Build succeeded
2019-01-30T15:05:26.712561+00:00 heroku[web.1]: Starting process with command `npm start`
2019-01-30T15:05:29.689631+00:00 app[web.1]:
2019-01-30T15:05:29.689652+00:00 app[web.1]: > devsocial@1.0.0 start /app
2019-01-30T15:05:29.689653+00:00 app[web.1]: > node server.js
2019-01-30T15:05:29.689655+00:00 app[web.1]:
2019-01-30T15:05:30.519539+00:00 heroku[web.1]: State changed from starting to up
2019-01-30T15:05:30.431624+00:00 app[web.1]: server running on port 6551
2019-01-30T15:05:30.864945+00:00 app[web.1]: MONGODB connected
2019-01-30T15:06:06.720079+00:00 heroku[router]: at=info method=GET path="/" host=developers-social-hub.herokuapp.com request_id=577ec493-3809-4d46-ac70-ab780e33a72a fwd="14.195.7.165" dyno=web.1 connect=0ms service=17ms status=200 bytes=3380 protocol=https
2019-01-30T15:06:10.882098+00:00 heroku[router]: at=info method=GET path="/static/js/1.41134588.chunk.js" host=developers-social-hub.herokuapp.com request_id=a23811f6-b586-4764-80bc-d1a5b0fa6a3f fwd="14.195.7.165" dyno=web.1 connect=0ms service=17ms status=200 bytes=262982 protocol=https
2019-01-30T15:06:13.705610+00:00 heroku[router]: at=info method=GET path="/static/media/showcase.4b31330b.png" host=developers-social-hub.herokuapp.com request_id=602db4e0-4689-4155-b0e2-add26b400107 fwd="14.195.7.165" dyno=web.1 connect=1ms service=20ms status=200 bytes=299564 protocol=https
2019-01-30T15:06:25.367222+00:00 heroku[router]: at=info method=GET path="/static/css/main.acfdd42c.chunk.css" host=developers-social-hub.herokuapp.com request_id=b74cc452-c66e-44b1-b204-0e0dd809ae7d fwd="14.195.7.165" dyno=web.1 connect=0ms service=3ms status=304 bytes=269 protocol=https
2019-01-30T15:06:25.519104+00:00 heroku[router]: at=info method=GET path="/static/js/1.41134588.chunk.js.map" host=developers-social-hub.herokuapp.com request_id=c0cecd73-f0d3-40bd-bd9a-c04a36fce781 fwd="14.195.7.165" dyno=web.1 connect=0ms service=40ms status=200 bytes=918681 protocol=https
2019-01-30T15:06:26.117461+00:00 heroku[router]: at=info method=GET path="/static/css/main.acfdd42c.chunk.css.map" host=developers-social-hub.herokuapp.com request_id=fa082de2-f36f-4b5a-a19f-e660ef78c710 fwd="14.195.7.165" dyno=web.1 connect=0ms service=3ms status=200 bytes=10189 protocol=https
2019-01-30T15:06:42.892519+00:00 heroku[router]: at=info method=GET path="/static/js/1.41134588.chunk.js.map" host=developers-social-hub.herokuapp.com request_id=3f1eba87-780e-4b79-a496-11a11ef8fc75 fwd="14.195.7.165" dyno=web.1 connect=1ms service=7ms status=206 bytes=384 protocol=https
2019-01-30T15:06:43.220431+00:00 heroku[router]: at=info method=GET path="/static/js/main.f484c008.chunk.js.map" host=developers-social-hub.herokuapp.com request_id=d4369096-b31f-4944-a8b9-8f58a4e49b24 fwd="14.195.7.165" dyno=web.1 connect=0ms service=6ms status=206 bytes=382 protocol=https
2019-01-30T15:06:43.548416+00:00 heroku[router]: at=info method=GET path="/static/css/main.acfdd42c.chunk.css.map" host=developers-social-hub.herokuapp.com request_id=6d734784-123c-47a7-8dca-792713e07048 fwd="14.195.7.165" dyno=web.1 connect=0ms service=3ms status=304 bytes=270 protocol=https
2019-01-30T15:06:47.007672+00:00 heroku[router]: at=info method=GET path="/login" host=developers-social-hub.herokuapp.com request_id=601e74b1-90fc-4c7c-8af6-97e22164dd75 fwd="14.195.7.165" dyno=web.1 connect=0ms service=5ms status=200 bytes=3380 protocol=https
2019-01-30T15:06:47.424786+00:00 heroku[router]: at=info method=GET path="/static/css/main.acfdd42c.chunk.css" host=developers-social-hub.herokuapp.com request_id=d62eb253-42e8-497b-9171-0a1bf24317a6 fwd="14.195.7.165" dyno=web.1 connect=0ms service=1ms status=304 bytes=269 protocol=https
2019-01-30T15:06:47.482306+00:00 heroku[router]: at=info method=GET path="/static/js/1.41134588.chunk.js" host=developers-social-hub.herokuapp.com request_id=e8066293-6e74-4912-93b7-b4e3d2c42b96 fwd="14.195.7.165" dyno=web.1 connect=0ms service=2ms status=304 bytes=271 protocol=https
2019-01-30T15:06:48.295582+00:00 heroku[router]: at=info method=GET path="/static/js/1.41134588.chunk.js.map" host=developers-social-hub.herokuapp.com request_id=b68e9180-9760-4a2f-b9d6-2aefe3811f93 fwd="14.195.7.165" dyno=web.1 connect=0ms service=1ms status=304 bytes=271 protocol=https
2019-01-30T15:21:27.000000+00:00 app[api]: Build started by user anujshrivastav97@gmail.com
2019-01-30T15:23:23.430119+00:00 heroku[web.1]: State changed from up to starting
2019-01-30T15:23:23.364252+00:00 heroku[web.1]: Restarting
2019-01-30T15:23:23.157037+00:00 app[api]: Release v24 created by user anujshrivastav97@gmail.com
2019-01-30T15:23:24.443008+00:00 heroku[web.1]: Stopping all processes with SIGTERM
2019-01-30T15:23:24.529467+00:00 heroku[web.1]: Process exited with status 143
2019-01-30T15:23:24.000000+00:00 app[api]: Build succeeded
2019-01-30T15:23:41.442643+00:00 app[web.1]:
2019-01-30T15:23:41.442661+00:00 app[web.1]: > devsocial@1.0.0 start /app
2019-01-30T15:23:41.442662+00:00 app[web.1]: > node server.js
2019-01-30T15:23:41.442664+00:00 app[web.1]:
2019-01-30T15:30:42.125999+00:00 heroku[router]: at=info method=GET path="/" host=developers-social-hub.herokuapp.com request_id=21465589-4731-4798-832f-af0c26bedbc2 fwd="14.195.7.165" dyno=web.1 connect=1ms service=39ms status=200 bytes=3380 protocol=https
2019-01-30T15:30:42.582894+00:00 heroku[router]: at=info method=GET path="/static/css/main.70695d27.chunk.css" host=developers-social-hub.herokuapp.com request_id=54b0b7dc-a8d8-4a49-a58c-7ccd831deb14 fwd="14.195.7.165" dyno=web.1 connect=1ms service=16ms status=200 bytes=3480 protocol=https
2019-01-30T15:30:42.608372+00:00 heroku[router]: at=info method=GET path="/static/js/1.41134588.chunk.js" host=developers-social-hub.herokuapp.com request_id=c3143587-0bbb-4335-84d6-699275ff85f2 fwd="14.195.7.165" dyno=web.1 connect=1ms service=24ms status=200 bytes=262982 protocol=https
2019-01-30T15:30:42.971407+00:00 heroku[router]: at=info method=GET path="/static/js/main.b0764334.chunk.js" host=developers-social-hub.herokuapp.com request_id=bb0ff852-f017-4a18-85b0-5fbef4b40f04 fwd="14.195.7.165" dyno=web.1 connect=1ms service=24ms status=200 bytes=81068 protocol=https
2019-01-30T15:30:48.547396+00:00 heroku[router]: at=info method=GET path="/static/media/showcase.4b31330b.png" host=developers-social-hub.herokuapp.com request_id=5358d70e-d6b0-4411-8854-e7faac6fdc63 fwd="14.195.7.165" dyno=web.1 connect=1ms service=22ms status=200 bytes=299564 protocol=https
2019-01-30T15:30:57.124715+00:00 heroku[router]: at=info method=GET path="/static/css/main.70695d27.chunk.css" host=developers-social-hub.herokuapp.com request_id=8cd68afc-b845-48f1-b44d-40bd90b46545 fwd="14.195.7.165" dyno=web.1 connect=1ms service=11ms status=304 bytes=269 protocol=https
2019-01-30T15:30:58.288270+00:00 heroku[router]: at=info method=GET path="/static/css/main.70695d27.chunk.css.map" host=developers-social-hub.herokuapp.com request_id=61cc2740-7076-4ed5-b453-6662c8719332 fwd="14.195.7.165" dyno=web.1 connect=1ms service=3ms status=200 bytes=10189 protocol=https
2019-01-30T16:06:00.798669+00:00 heroku[web.1]: Idling
2019-01-30T16:06:02.020251+00:00 heroku[web.1]: Stopping all processes with SIGTERM
2019-01-30T16:06:02.110545+00:00 heroku[web.1]: Process exited with status 143
2019-01-30T17:11:32.000000+00:00 app[api]: Build succeeded
2019-01-30T17:11:49.442977+00:00 app[web.1]:
2019-01-30T17:11:49.442994+00:00 app[web.1]: > devsocial@1.0.0 start /app
2019-01-30T17:11:49.442996+00:00 app[web.1]: > node server.js
2019-01-30T17:11:49.442997+00:00 app[web.1]:
2019-01-30T17:11:50.551439+00:00 heroku[web.1]: State changed from starting to up
2019-01-30T17:11:50.953625+00:00 app[web.1]: MONGODB connected
2019-01-30T17:13:06.463986+00:00 heroku[router]: at=info method=GET path="/" host=developers-social-hub.herokuapp.com request_id=f23aa0c6-a1a7-4ceb-bef0-0f5b4398cb34 fwd="14.195.248.32" dyno=web.1 connect=1ms service=24ms status=200 bytes=3380 protocol=https
2019-01-30T17:13:06.976791+00:00 heroku[router]: at=info method=GET path="/static/js/1.41134588.chunk.js" host=developers-social-hub.herokuapp.com request_id=de2903e1-4e61-4a1f-9720-aa428f145e82 fwd="14.195.248.32" dyno=web.1 connect=0ms service=21ms status=200 bytes=262982 protocol=https
2019-01-30T17:13:07.304140+00:00 heroku[router]: at=info method=GET path="/static/js/main.47a72df7.chunk.js" host=developers-social-hub.herokuapp.com request_id=0c23cf4c-8ac5-4e7f-8e3e-c1fc35fbe29c fwd="14.195.248.32" dyno=web.1 connect=1ms service=14ms status=200 bytes=81068 protocol=https
2019-01-30T17:13:10.848697+00:00 heroku[router]: at=info method=GET path="/static/media/showcase.4b31330b.png" host=developers-social-hub.herokuapp.com request_id=da937da9-ef19-4efa-bb35-5fd6b7f6d76c fwd="14.195.248.32" dyno=web.1 connect=0ms service=14ms status=200 bytes=299564 protocol=https
2019-01-30T17:13:27.358952+00:00 heroku[router]: at=info method=GET path="/static/css/main.6d977267.chunk.css" host=developers-social-hub.herokuapp.com request_id=8e19d292-15ee-412c-ae31-c9526c8d00ef fwd="14.195.248.32" dyno=web.1 connect=0ms service=2ms status=304 bytes=269 protocol=https
2019-01-30T17:13:27.788316+00:00 heroku[router]: at=info method=GET path="/static/js/main.47a72df7.chunk.js.map" host=developers-social-hub.herokuapp.com request_id=3761b533-0220-4153-bcaf-95bb2e1332c7 fwd="14.195.248.32" dyno=web.1 connect=0ms service=10ms status=200 bytes=172894 protocol=https
2019-01-30T17:50:01.612113+00:00 heroku[web.1]: State changed from up to down
2019-01-30T17:53:04.950094+00:00 heroku[web.1]: State changed from down to starting
2019-01-30T17:53:16.944691+00:00 heroku[web.1]: Starting process with command `npm start`
2019-01-30T17:53:21.522905+00:00 app[web.1]: server running on port 38717
2019-01-30T17:53:21.998654+00:00 app[web.1]: MONGODB connected
2019-01-31T10:57:36.000000+00:00 app[api]: Build started by user anujshrivastav97@gmail.com
2019-01-31T10:59:20.858952+00:00 heroku[web.1]: State changed from down to starting
2019-01-31T10:59:20.622923+00:00 app[api]: Release v26 created by user anujshrivastav97@gmail.com
2019-01-31T10:59:20.622923+00:00 app[api]: Deploy 230e9c11 by user anujshrivastav97@gmail.com
2019-01-31T10:59:21.000000+00:00 app[api]: Build succeeded
2019-01-31T10:59:49.011384+00:00 heroku[web.1]: Starting process with command `npm start`
2019-01-31T10:59:52.249315+00:00 app[web.1]: server running on port 28879
2019-01-31T10:59:52.807931+00:00 heroku[web.1]: State changed from starting to up
2019-01-31T10:59:52.692643+00:00 app[web.1]: MONGODB connected
2019-01-31T11:00:12.663062+00:00 heroku[router]: at=info method=GET path="/" host=developers-social-hub.herokuapp.com request_id=baeb6f34-03db-43b9-a136-0797a2aa5dbc fwd="103.70.96.36" dyno=web.1 connect=1ms service=29ms status=200 bytes=3380 protocol=https
2019-01-31T11:00:13.198581+00:00 heroku[router]: at=info method=GET path="/static/js/main.fdf74e5f.chunk.js" host=developers-social-hub.herokuapp.com request_id=9f592338-885d-4553-875f-12b3a9486eb4 fwd="103.70.96.36" dyno=web.1 connect=0ms service=7ms status=200 bytes=81068 protocol=https
2019-01-31T11:00:18.802213+00:00 heroku[router]: at=info method=GET path="/static/media/showcase.4b31330b.png" host=developers-social-hub.herokuapp.com request_id=760ba03c-b0cd-4d07-acfe-cf040f959ba6 fwd="103.70.96.36" dyno=web.1 connect=1ms service=29ms status=200 bytes=299564 protocol=https
2019-01-31T11:00:24.266297+00:00 heroku[router]: at=info method=GET path="/favicon.ico" host=developers-social-hub.herokuapp.com request_id=c6a66f6b-1853-40da-bfa8-cbd2222d7d9f fwd="103.70.96.36" dyno=web.1 connect=1ms service=9ms status=200 bytes=4179 protocol=https
2019-01-31T11:00:24.944477+00:00 heroku[router]: at=info method=GET path="/manifest.json" host=developers-social-hub.herokuapp.com request_id=f2f6b309-cba6-4801-9622-b3790688aa3b fwd="103.70.96.36" dyno=web.1 connect=1ms service=2ms status=200 bytes=633 protocol=https
2019-01-31T11:00:33.453884+00:00 heroku[router]: at=info method=GET path="/static/css/main.e24c73be.chunk.css" host=developers-social-hub.herokuapp.com request_id=94ae1ecc-6976-4065-8b5c-03df58b6c33b fwd="103.70.96.36" dyno=web.1 connect=1ms service=4ms status=304 bytes=269 protocol=https
2019-01-31T11:00:33.554518+00:00 heroku[router]: at=info method=GET path="/static/js/main.fdf74e5f.chunk.js.map" host=developers-social-hub.herokuapp.com request_id=92f5041b-769d-4175-bcc1-38ec1ab6f99b fwd="103.70.96.36" dyno=web.1 connect=1ms service=15ms status=200 bytes=172894 protocol=https
2019-01-31T11:00:33.982950+00:00 heroku[router]: at=info method=GET path="/static/css/main.e24c73be.chunk.css.map" host=developers-social-hub.herokuapp.com request_id=e966818d-1ba7-48c9-9415-6bc8671d50e8 fwd="103.70.96.36" dyno=web.1 connect=1ms service=3ms status=200 bytes=10189 protocol=https
2019-01-31T11:00:55.000000+00:00 app[api]: Build started by user anujshrivastav97@gmail.com
2019-01-31T11:02:51.952606+00:00 heroku[web.1]: Restarting
2019-01-31T11:02:51.955452+00:00 heroku[web.1]: State changed from up to starting
2019-01-31T11:02:51.374765+00:00 app[api]: Deploy b89dd4ff by user anujshrivastav97@gmail.com
2019-01-31T11:02:51.374765+00:00 app[api]: Release v27 created by user anujshrivastav97@gmail.com
2019-01-31T11:02:52.000000+00:00 app[api]: Build succeeded
2019-01-31T11:03:12.521963+00:00 app[web.1]:
2019-01-31T11:03:12.521998+00:00 app[web.1]: > devsocial@1.0.0 start /app
2019-01-31T11:03:12.522000+00:00 app[web.1]: > node server.js
2019-01-31T11:03:12.522002+00:00 app[web.1]:
2019-01-31T11:03:14.294077+00:00 app[web.1]: server running on port 50551
2019-01-31T11:03:14.757626+00:00 app[web.1]: MONGODB connected
2019-01-31T11:03:23.904534+00:00 heroku[router]: at=info method=GET path="/static/css/main.acc87c5e.chunk.css" host=developers-social-hub.herokuapp.com request_id=503729fc-0aff-4ef6-aa53-872336b7e43e fwd="103.70.96.36" dyno=web.1 connect=0ms service=5ms status=200 bytes=3480 protocol=https
2019-01-31T11:03:24.146614+00:00 heroku[router]: at=info method=GET path="/static/js/1.41134588.chunk.js" host=developers-social-hub.herokuapp.com request_id=0f42c6e0-faed-46b3-84d7-3169d317e699 fwd="103.70.96.36" dyno=web.1 connect=0ms service=20ms status=200 bytes=262982 protocol=https
2019-01-31T11:03:24.433598+00:00 heroku[router]: at=info method=GET path="/static/css/main.acc87c5e.chunk.css.map" host=developers-social-hub.herokuapp.com request_id=168de6b1-dc97-482b-9606-7d10b54d68ec fwd="103.70.96.36" dyno=web.1 connect=0ms service=7ms status=200 bytes=10189 protocol=https
2019-01-31T11:03:27.923062+00:00 heroku[router]: at=info method=GET path="/static/js/main.e5514eee.chunk.js.map" host=developers-social-hub.herokuapp.com request_id=d0e9db73-d38c-4fcb-85de-6688ad9d2d7f fwd="103.70.96.36" dyno=web.1 connect=0ms service=33ms status=200 bytes=172894 protocol=https
2019-01-31T11:03:28.666443+00:00 heroku[router]: at=info method=GET path="/manifest.json" host=developers-social-hub.herokuapp.com request_id=935a0817-a7ab-4c69-a71d-091e05f77f60 fwd="103.70.96.36" dyno=web.1 connect=1ms service=11ms status=200 bytes=633 protocol=https

0 个答案:

没有答案