Hello,
I am trying to get my express app working in production, it is called server.js and located in my functions folder:
// functions/server.js
const connectDB = require('../config/db');
const express = require('express');
const serverless = require('serverless-http');
const app = express();
const basePath = '/.netlify/functions/server';
// Connect Database
connectDB();
// Init Middleware
app.use(express.json({ extended: false }));
// Define routes
app.use(`${basePath}/auth`, require('./routes/auth'));
module.exports.handler = serverless(app);
And here is config/db.js
// config/db.js
const mongoose = require('mongoose');
const config = require('config');
const db = config.get('mongoURI');
const options = {
useFindAndModify: false,
useCreateIndex: true,
useNewUrlParser: true,
useUnifiedTopology: true,
};
const connectDB = async () => {
try {
await mongoose.connect(db, options);
console.log('MongoDB connected.');
} catch(err) {
console.error(err.message);
process.exit(1);
};
};
module.exports = connectDB;
This is config/default.json
{
"mongoURI": "Value removed for post",
"jwtSecret": "Value removed for post"
}
And finally, a very simplified version of functions/routes/auth.js
// functions/routes/auth.js
const express = require('express');
const router = express.Router();
const bcrypt = require('bcryptjs');
const auth = require('../middleware/auth');
const User = require('../models/User');
const jwt = require('jsonwebtoken');
const config = require('config');
const { check, validationResult } = require('express-validator');
// @route GET /.netlify/functions/server/auth
// @desc Authenticate provided user credentials
// @access Public
router.get('/', auth, async (req, res) => {
try {
const user = await User.findById(req.user.id).select('-password');
res.json(user);
} catch(err) {
res.status(500).send('Server error.');
}
});
This is my /netlify.toml file where the built-functions folder is called âlambdaâ
[build]
command = "yarn build"
functions = "lambda"
publish = "build"
My /jsconfig.json file in the project root:
{
"compilerOptions": {
"baseUrl": "src"
},
"include": ["src"]
}
And start/build/deploy scripts in /package.json
"scripts": {
"start": "run-p start:**",
"start:app": "react-scripts start",
"start:server": "netlify-lambda serve functions",
"build": "run-s build:**",
"build:app": "react-scripts build",
"build:server": "netlify-lambda build functions",
"test": "react-scripts test",
"eject": "react-scripts eject",
"deploy": "yarn build && netlify deploy --prod"
}
Edit: Almost forgot if you want a 1:1 to get up and running locally with basic create-react-app, this is src/setupProxy.js
// src/setupProxy.js
const proxy = require('http-proxy-middleware');
module.exports = app => {
const basePath = '/.netlify/functions/server';
app.use(
basePath,
proxy({ target: 'http://localhost:9000' })
);
};
I am facing two issues:
- my /config folder in my local project root does not seem to be available after deployment to Netlify â this path was being used to get my mongoURI and jwtSecret when connecting the db via express â the app was bootstrapped with Create React App, if that helps and is not ejected.
Edit: In production, response status code is 502 with the following:
errorMessage: "Configuration property "mongoURI" is not defined"
- After manually adding database values directly in the express portion, to temporarily bypass issue 1, the API times out after 10 seconds every time the express app lambda function is invoked in production (/.netlify/functions/server). This does not occur in the local development environment, where everything works as expected.
Edit:In production, response status code is 502 with the following:
errorMessage: "2020-01-02T22:27:59.500Z 5d1db543-e1e4-4ade-8f15-fe05c534757b Task timed out after 10.01 seconds"
Please keep in mind this is Express/mongoose and not using the native lambda event, context, callback.
Edit: Using Node v10.16.3
Any insight will be helpful â thanks.