简体   繁体   中英

How to organize a website that runs on Node-express server?

I'm quite new to this. What I've got now is an index.html and record.html , a application.js file that contains All the logics and handles XMLhttp responses, and a style.css file.

Below is my node-express server. Currently it runs locally. It will need to be deployed to AWS later. My question is, what's the proper way to organize this project? Is this ok to just keep html , js and css in a public folder and keep the node server files together? I'm not coding any javascript in node server, is this an ok practice? Many thanks in advance!

Server:

app.use(express.static(__dirname+'/public'));
app.get('/record', function(req, res) {
       res.sendFile(path.join(__dirname + '/public'+ '/record.html'));
});


app.get('/', function (req, res) {
       fs.readFile('/index.html', function(error, content) {
          if (error) {
          res.writeHead(500);
          res.end();
       } else {
        res.writeHead(200, { 'Content-Type': 'text/html' });
        res.end(content, 'utf-8');
    }
}); 
  res.send('Hello World');   
});

https.createServer({
   key: privateKey,
   cert: certificate
}, app).listen(8080);

httpServer.listen(8443);

It seems like you just need an HTTP server.

All the static content should be served from the public folder, so you are doing it right. If your public folder contains an index.html file, it should open up when you visit http://localhost:8080

// Load required packages
var express = require('express');

// Create our Express application
var app = express();

// Add static middleware
app.use(express.static(__dirname + '/public'));

// Create our Express router
var router = express.Router();

// Initial dummy route for testing
router.get('/', function(req, res) {

});

// Register all our routes
app.use(router);

// Start the server
app.listen(8080);

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