menu
up-caret
down-caret

ZEIT

Our mission is to make cloud computing as easy and accessible as mobile computing. You can find our Next.js community here.

# General

General Chatter

Trending conversations
503 on deployment
@codelayer · 22d
ENV variables
@remotealex · 4h
BRU1 instances are timing out
@helguita · 31d
Could someone point me toward strategies for decreasing my PHP lambda size?
@unitof · 5h
Images not showing on CRA deployment
@passingmagic · 5h

Haven't deployed in a few months, same project not deployable.

ZEIT/General · February 22, 2019 at 6:35am

Haven't deployed in a few months, same project not deployable.

February 22, 2019 at 6:35am

I have a node.js deployment that worked fine until today when I tried to deploy a very minor change. Instead of my server-side code in my /server/ directory deploying to /api and my client-side code in my /dist/ folder deploying to /, all I saw on deployment was a directory of my files. I tried adding "builds": [{ "src": "*.js", "use": "@now/node" }] to my now.json file but that still showed my a directory on deploy, it just happened to be a different directory. I'm not sure what changes I would have to make to get my site to deploy properly. All the routing is handled in my router and when i npm start locally, it works just fine.


February 22, 2019 at 7:10am

I tried it with the following now.json configuration and the static file gets served with no issue, but if I try to access my api, ie: /api/vendors ... I get a 404. I've tried a bunch of combinations ie: using src: *.js and for api routes i've tried stuff like /api/vendors mapping to dest /server/api/vendors.js but nothing seems to work for me. I have a root server.js which defines routes to the client at /dist/index.html and defines api routes in /server/routes/api/vendors.js and /server/routes/api/orders.js ... i also have /server/server.js and /server/routes/index.js files { "name": "myapp", "alias": "app.myapp.com", "version": 2, "builds": [ { "src": "/dist/index.html", "use": "@now/static" }, { "src": "server.js", "use": "@now/node" } ], "routes": [ { "src": "/", "headers": {"cache-control": "s-maxage=1000"}, "dest": "/dist/index.html" }, { "src": "/api", "dest": "/server/server.js" } ] }

  • reply
  • like

this is what my folder structure looks like

  • reply
  • like
  • reply
  • like

Hello! In the past did you deploy to Now 2.0 or Now 1.0?

  • reply
  • like

originally the project was 1.0 but i'm pretty sure i made the switch to 2.0 when it became available, in order to get faster, more reliable deployments.

  • reply
  • like

February 25, 2019 at 4:55pm

any ideas/suggestions?

  • reply
  • like

Please, share some code with us so we can take a look at what went wrong.

  • reply
  • like

server.js

const nodemon = require('nodemon');
const path = require('path');
nodemon({
execMap: {
js: 'node'
},
script: path.join(\_\_dirname, 'server/server'),
ignore: [],
watch: process.env.NODE\_ENV !== 'production' ? ['server/\*'] : false,
ext: 'js'
})
.on('restart', function() {
console.log('Server restarted!');
})
.once('exit', function () {
console.log('Shutting down server');
process.exit();
});
Edited
  • reply
  • like

server/server.js

const express = require('express');
const bodyParser = require('body-parser');
const cors = require('cors');
const fs = require('fs');
const historyApiFallback = require('connect-history-api-fallback');
const mongoose = require('mongoose');
const path = require('path');
// const webpack = require('webpack');
// const webpackDevMiddleware = require('webpack-dev-middleware');
// const webpackHotMiddleware = require('webpack-hot-middleware');
const config = require('../config/config');
// const webpackConfig = require('../webpack.config');
const isDev = process.env.NODE_ENV !== 'production';
// const isDev = false;
const port = process.env.PORT || 8081;
// Configuration
// ================================================================================================
// Set up Mongoose
var options = {
server: { socketOptions: { keepAlive: 1, connectTimeoutMS: 30000 } },
replset: { socketOptions: { keepAlive: 1, connectTimeoutMS: 30000 } }
};
mongoose.connect(isDev ? config.db_dev : config.db, options);
mongoose.Promise = global.Promise;
const app = express();
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
app.use(bodyParser.json());
app.use(cors());
// API routes
require('./routes')(app);
app.use(express.static(path.resolve(__dirname, '../dist/')));
app.get('/', function (req, res) {
res.sendFile(path.resolve(__dirname, '../dist/index.html'));
console.log("GDres: "+res);
res.end();
// next();
});
app.listen(port, '0.0.0.0', (err) => {
if (err) {
console.log(err);
}
console.info('>>> 🌎 Open http://0.0.0.0:%s/ in your browser.', port);
});
module.exports = app;
  • reply
  • like

server/routes/index.js

const fs = require('fs');
const path = require('path');
module.exports = (app) => {
// API routes
fs.readdirSync(__dirname + '/api/').forEach((file) => {
require(`./api/${file.substr(0, file.indexOf('.'))}`)(app);
});
};
  • reply
  • like

server/routes/api/vendors.js

const Vendor = require('../../models/Vendor');
module.exports = (app) => {
app.get('/api/vendors', (req, res, next) => {
Vendor.find()
.exec()
.then((vendor) => res.json(vendor))
.catch((err) => next(err));
});
app.get('/api/vendors/location/:lat/:long', (req, res, next) => {
Vendor.find()
.exec()
.then((vendor) => res.json(vendor))
.catch((err) => next(err));
});
app.get('/api/vendors/:email', (req, res, next) => {
Vendor.findOne({ email: req.params.email })
.exec()
.then((vendor) => res.json(vendor))
.catch((err) => next(err));
});
app.post('/api/vendors', function (req, res, next) {
const vendor = new Vendor(req.body);
console.log(req.body);
console.log(vendor);
vendor.save()
.then((vendor) => res.json(vendor))
// .then(item => res.send("item saved to database"))
.catch((err) => next(err));
});
app.delete('/api/vendors/:id', function (req, res, next) {
Vendor.findOneAndRemove({ _id: req.params.id })
.exec()
.then((vendor) => res.json())
.catch((err) => next(err));
});
app.put('/api/vendors/:id/update/:capacity', (req, res, next) => {
Vendor.findById(req.params.id)
.exec()
.then((vendor) => {
vendor.capacity = req.params.capacity;
vendor.save()
.then(() => res.json(vendor))
.catch((err) => next(err));
})
.catch((err) => next(err));
});
app.put('/api/vendors/:id/updateEmail/:email', (req, res, next) => {
Vendor.findById(req.params.id)
.exec()
.then((vendor) => {
vendor.email = req.params.email;
vendor.save()
.then(() => res.json(vendor))
.catch((err) => next(err));
})
.catch((err) => next(err));
});
};
  • reply
  • like

server.js

const nodemon = require('nodemon');
const path = require('path');
nodemon({
execMap: {
js: 'node'
},
script: path.join(\_\_dirname, 'server/server'),
ignore: [],
watch: process.env.NODE\_ENV !== 'production' ? ['server/\*'] : false,
ext: 'js'
})
.on('restart', function() {
console.log('Server restarted!');
})
.once('exit', function () {
console.log('Shutting down server');
process.exit();
});

This will never work, since we are in a lambda environment.

  • reply
  • like

What is the content of your nowj.son?

  • reply
  • like

I've tried a few different flavors of this

{
"name": "myapp",
"alias": "app.myapp.com",
"version": 2,
"builds": [
{ "src": "/dist/index.html", "use": "@now/static" },
{ "src": "server.js", "use": "@now/node" }
],
"routes": [
{ "src": "/", "headers": {"cache-control": "s-maxage=1000"}, "dest": "/dist/index.html" },
{ "src": "/api", "dest": "/server/server.js" }
]
}

I guess my best option at this point is to take one of the examples as a starting point and rebuild my api. what specifically about my code does not work in a lambda environment?

  • reply
  • like

I think this route is probably what is holding you back:

{ "src": "/api", "dest": "/server/server.js" }

It should be { "src": "/api/(.*)", "dest": "/server/server.js" } in order to catch all routes that starts with /api. In your original route above, it only matches /api.

  • reply
  • like

how would i test the monorepo locally?

  • reply
  • like

The answer for that is now dev, which is on the works! https://github.com/zeit/now-cli/pull/1883

  • reply
  • like

so there is no way to test any web application locally until now dev is released?

  • reply
  • like

so you're saying if i get rid of the nodemon code in my application i should be able to deploy it in the lambda environment? i tried that now.json configuration with no luck.

  • reply
  • like

so there is no way to test any web application locally until now dev is released?

You can use your own local solution for now, depending on the builder of course...

  • reply
  • like

so you're saying if i get rid of the nodemon code in my application i should be able to deploy it in the lambda environment? i tried that now.json configuration with no luck.

No, I am saying that nodemon doesn't make sense in a Now environment since deployments are immutable.

  • reply
  • like

Please, take a look at our examples here: https://github.com/zeit/now-examples

  • reply
  • like

Thanks...i was able to rebuild the API using the example. If i run npm start, which in turn runs node index.js, is there a default port it's using or do i need to specify a port in order to test hitting the API locally?

  • reply
  • like

or do I just need to add an app.listen(port, '0.0.0.0') statement?

  • reply
  • like
Show more messages