He hecho una aplicación básica en Node.JS Estoy tratando de implementarlo en el servidor heroku que se implementó con éxito.
Puedo acceder a esta aplicación en localhost, sin embargo, no se ejecuta en el servidor heroku . Intenta iniciar y luego mata automáticamente.
índice.js
var http = require("http"); http.createServer(function (request, response) { // Send the HTTP header // HTTP Status: 200 : OK // Content Type: text/plain response.writeHead(200, {'Content-Type': 'text/plain'}); // Send the response body as "Hello World" response.end('Hello World\n'); }).listen(80);paquete.json
{ "name": "powerful-escarpment", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "start": "node index.js" }, "author": "", "license": "ISC" }servidor.logs
2017-04-27T15:05:52.119923+00:00 heroku[web.1]: State changed from crashed to starting 2017-04-27T15:05:53.414613+00:00 heroku[web.1]: Starting process with command `npm start` 2017-04-27T15:05:56.563672+00:00 app[web.1]: 2017-04-27T15:05:56.563686+00:00 app[web.1]: > powerful-escarpment@1.0.0 start /app 2017-04-27T15:05:56.563687+00:00 app[web.1]: > node index.js 2017-04-27T15:05:56.563687+00:00 app[web.1]: 2017-04-27T15:05:56.836819+00:00 heroku[web.1]: Process exited with status 0 2017-04-27T15:05:56.849642+00:00 heroku[web.1]: State changed from starting to crashedSu aplicación falla porque está utilizando el puerto 80 estático. Por defecto, Heroku no proporciona el puerto 80 (no sé por qué, puede ser porque es bien conocido). El entrenamiento es usar un número de puerto dinámico.
Aquí una aplicación simple que creé hace un momento ( https://infinite-savannah-21740.herokuapp.com/ )
Perfil:
web: node index.jsÍndice.js
const express = require('express'); const path = require('path'); // Server var server = express(); var port = process.env.PORT || 8080; // <== this is must server.get('/', (req, res) => { res.send("Working") }) server.listen(port, () => { console.log("Listening on port: " + port) })Creo que eso se debe a que está definiendo el puerto de manera incorrecta. Debería ser algo como,
var http = require("http"); http.createServer(function (request, response) { // Send the HTTP header // HTTP Status: 200 : OK // Content Type: text/plain response.writeHead(200, {'Content-Type': 'text/plain'}); // Send the response body as "Hello World" response.end('Hello World\n'); }).listen(process.env.PORT || 3000);La verdad yo tambien soy novato pero me funciona
Heroku establece el puerto en el que se ejecuta su aplicación y luego lo vincula al puerto 80. Para obtener el puerto que establece Heroku, debe leer process.env.PORT :
const http = require("http"); const port = process.env.PORT || 80; http.createServer(function (request, response) { // Send the HTTP header // HTTP Status: 200 : OK // Content Type: text/plain response.writeHead(200, {'Content-Type': 'text/plain'}); // Send the response body as "Hello World" response.end('Hello World\n'); }).listen(port);