I am trying to modify the entry point file index.js in the create-react-app pre-configured project. I am trying to connect the project to mongodb, add a body-parser and api file in index.js but I get an error when I edit index.js.
Index.js:
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
ReactDOM.render(App, document.getElementById('root'));
My code:
const express = require('express');
const app = express()
const bodyParser = require('body-parser')
const mongoose = require('mongoose')
mongoose.connect('mongodb://localhost/treedb')
mongoose.Promise = global.Promise
app.use(bodyParser.json())
//initialize routes
app.use('/api', require('./routes/api'))
Error:
TypeError: Cannot read property 'prototype' of undefined
in file node_modules/express/lib/response.js:58:
var res = Object.create(http.ServerResponse.prototype);
This error occurs when I start the app with "npm start" and "react-scripts start" is run. My code is fine when I run "node index.js" but the import statements cause an error instead. Any ideas how to edit the index.js file without causing an error? Thanks.