neverendingqs / netlify-express

Express.js hosted on Netlify
https://netlify-express.netlify.app/
Apache License 2.0
321 stars 342 forks source link

Details about /.netlify Folder #23

Closed yuripramos closed 4 years ago

yuripramos commented 4 years ago

Hey, how you are you? I hope you are doing great during this quarantine like me =)

So, I'm trying to implement this solution in my repo, a simple repo that runs in express and I'm developing some edits to my index.js file according this post: https://www.netlify.com/blog/2018/09/13/how-to-run-express.js-apps-with-netlify-functions/#trying-it-out and you repo:

I'm facing some issue to get info about the .netlify folder that you write inside express/server.js

Here's my code highlighted with your approach:

require('dotenv').config()
const ACCESS_TOKEN = process.env.CLIENT_ACCESS_TOKEN;
const APIAI_SESSION_ID = process.env.DEV_ACCESS_TOKEN;

const express = require('express');
const app = express();
const path = require('path');
const serverless = require('serverless-http');
const bodyParser = require('body-parser');
const router = express.Router();

app.use(express.static(__dirname + '/views')); // html
app.use(express.static(__dirname + '/public')); // js, css, images

//HERE IS MY ISSUE
app.use('/.netlify/functions/server', router);  // path must route to lambda

const server = app.listen(process.env.PORT || 5000, () => {
  console.log('Express server listening on port %d in %s mode', server.address().port, app.settings.env);
});

const apiai = require('apiai')(ACCESS_TOKEN);

//WEB UI
app.get('/', (req, res) => {
  res.sendFile('index.html');
});

const io = require('socket.io')(server);

io.on('connection', function (socket) {
  socket.on('chat message', (text) => {

    // Get a reply from API.AI

    let apiaiReq = apiai.textRequest(text, {
      sessionId: APIAI_SESSION_ID
    });

    apiaiReq.on('response', (response) => {
      let aiText = response.result.fulfillment.speech;
      socket.emit('bot reply', aiText); // Send the result back to the browser!
    });

    apiaiReq.on('error', (error) => {
      console.log(error);
    });

    apiaiReq.end();

  });
});

module.exports = app;
module.exports.handler = serverless(app);

Glad if you could give me some guidance, thanks!

yuripramos commented 4 years ago

Netlify doesn't work very well with an express server without serverless, I couldn't be able to solve and the community didn't answer my question, I solved this problem using Heroku server.

Just to help many others in the future, don't waste too much time trying to configure your express with netlify, just go with Heroku