Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
400 views
in Technique[技术] by (71.8m points)

node.js - How do I deploy an server-side rendered app to cpanel?

I have a project that uses Express and EJS. I added the necessary project files to my public_html folder and setup a node.js app with cpanel. I cannot get it to render the site. So far I've set up an http server but I can only get it to render text like "hello world" using res.end('Hello world). Passing it the express instance does nothing. I cannot find any documentation for this kind of setup and talking to my host CS is like talking to toddlers, so please help. This is my server.js file atm:

require('dotenv').config();
let config = require(__dirname + '/config/config.js')['production'];
const cookieParser = require('cookie-parser');
const db = require('./models');
const express = require('express');
const flash = require('connect-flash');
const http = require('http');
const layouts = require('express-ejs-layouts');
const methodOverride = require('method-override');
const passport = require('./config/ppConfig.js');
const path = require('path');
const session = require('express-session');
const app = express();

app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');

app.use(require('morgan')('dev'));
app.use(
  express.urlencoded({
    extended: false,
  })
);

app.use(express.static(__dirname + '/public'));
app.use(layouts);
app.use(methodOverride('_method'));

// Session config
app.use(cookieParser());

app.use(
  session({
    secret: 'secret',
    resave: false,
    saveUninitialized: true,
    cookie: {
      sameSite: 'strict',
    },
  })
);

app.use(flash());

app.use(passport.initialize());
app.use(passport.session());

app.use((req, res, next) => {
  res.locals.alerts = req.flash();
  res.locals.currentUser = req.user;
  next();
});

// Routes

app.get('/', (req, res) => {
  const locals = {
    title: '404AnswersNotFound',
    description: 'Where answers are not found, but found.',
    style: '/css/home.css',
    isUserLoggedIn: false,
  };
  locals.isUserLoggedIn = !!req.user;

  db.question.findAll({ limit: 3 }).then(question => {
    db.answer
      .findAll({ limit: 3 })
      .then(answer => {
        db.category
          .findAll()
          .then(category => {
            res.render('index', {
              meta: locals,
              questions: question,
              answers: answer,
              cat: category,
            });
          })
          .catch(err => {
            console.log(err);
          });
      })
      .catch(err => {
        console.log(err);
      });
  });
});

app.use('/auth', require('./controllers/auth'));
app.use('/inquire', require('./controllers/inquire'));
app.use('/profile', require('./controllers/profiles'));

const server = http.createServer((req, res) => {
  res.writeHead(200, {'Content-Type': 'text/html'});
  res.end('Hello world');
});

server.listen();

module.exports = server;
question from:https://stackoverflow.com/questions/65914133/how-do-i-deploy-an-server-side-rendered-app-to-cpanel

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...