简体   繁体   中英

How to give root access to Heroku Procfile?

I am making a website with NodeJS, an I'm working on adding HTTPS support to it.However, when I publish it to Heroku, I get the error "Error: listen EACCES: permission denied 0.0.0.0:443". I saw that this is because it needs root access to run an HTTP server. Is there a way to give root privileges to the Procfile process? this is my server code:

import https from "https";
import http from "http";
import node_static from "node-static"
import { headers } from './util/helpers.mjs'
import home from './pages/home.mjs'
import e404 from "./pages/404.mjs";
import * as fs from 'fs';
const PORT = "8000";
let fileServ = new node_static.Server('./public');
let publicd;
fs.readdir("./public", (err,files)=>{
    if (err){
        console.error(err);
    } else {
        publicd=files;
    };
});
const options={
    key: fs.readFileSync('./cert/CA/192.168.0.22/192.168.0.22.decrypted.key'),
    cert: fs.readFileSync('./cert/CA/192.168.0.22/192.168.0.22.crt')
};
(async () => {
    let httpsServer=https.createServer(options, async (req, res) => {
        try{
            fileServ.serve(req, res);
            if (publicd.find(item =>{
                if (item===req.url.split('/')[1]){
                    return true;
                }else{
                    return false;
                }
            })){}else{
                switch (req.url) {
                    case '/':
                        headers(home, res);
                        break;
                    default:
                        res.writeHead(404, 'NOT FOUND',{
                            'Content-Type': 'text/html',
                            'Content-Length': e404.length,
                            'Expires': new Date().toUTCString()
                        });
                        res.end(e404)
                }
            }
        }catch(err){
            res.writeHead(500,'ERROR');
            res.end('Error 500');
            console.error(err);
        };
        let today = new Date();
        let statusMessage;
        if (res.statusMessage){
            statusMessage=res.statusMessage;
        } else {
            statusMessage="FILE" //File server gives no status message.
        };
        let date = `${today.getFullYear()}-${(today.getMonth()+1)}-\
${today.getDate()} ${today.getHours()}:${today.getMinutes()}`
        console.log(`HTTPS: ${date} ${statusMessage} ${req.url} ${res.statusCode}`);
    });
    let httpServer=http.createServer(async (req, res) => {
        try{
            fileServ.serve(req, res);
            if (publicd.find(item =>{
                if (item===req.url.split('/')[1]){
                    return true;
                }else{
                    return false;
                }
            })){}else{
                switch (req.url) {
                    case '/':
                        headers(home, res);
                        break;
                    default:
                        res.writeHead(404, 'NOT FOUND',{
                            'Content-Type': 'text/html',
                            'Content-Length': e404.length,
                            'Expires': new Date().toUTCString()
                        });
                        res.end(e404)
                }
            }
        }catch(err){
            res.writeHead(500,'ERROR');
            res.end('Error 500');
            console.error(err);
        };
        let today = new Date();
        let statusMessage;
        if (res.statusMessage){
            statusMessage=res.statusMessage;
        } else {
            statusMessage="FILE" //File server gives no status message.
        };
        let date = `${today.getFullYear()}-${(today.getMonth()+1)}-\
${today.getDate()} ${today.getHours()}:${today.getMinutes()}`
        console.log(`HTTP: ${date} ${statusMessage} ${req.url} ${res.statusCode}`);
    });
    httpsServer.listen(443);
    httpServer.listen(80)
})()

Is there a way to do that, or do I have to only use HTTP for my website? I have to run it as root on my local computer anyways.

How to give root access to Heroku Procfile?

You don't.

Your app shouldn't know anything about HTTPS, certificate files, etc. Let Heroku handle that. Just listen for HTTP (not HTTPS) connections on the port it gives you via the PORT environment variable.

Heroku runs HTTPS automatically on the default yourapp.herokuapp.com domain. If you want HTTPS on a custom domain, don't try to handle the encrypted connection yourself. Either use ACM (easiest, but requires a paid dyno) or bring your own cert with Heroku SSL .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM