Skip to content

Instantly share code, notes, and snippets.

@codersidprogrammer
Created January 27, 2023 03:13
Show Gist options
  • Save codersidprogrammer/c282679435425e4d213bc583b2923623 to your computer and use it in GitHub Desktop.
Save codersidprogrammer/c282679435425e4d213bc583b2923623 to your computer and use it in GitHub Desktop.
Proses pembuatan server untuk deploy angular
/**
*
* filename: server.js
*
*
* Ini yang akan di eksekusi oleh server
* Running step:
* 1. ng build --build-optimizer
* 2. Check folder dist (setting path dist ada di angular.json)
* 3. running server node server.js
*/
const express = require("express");
const path = require("path");
const fs = require("fs");
const app = express();
const staticRoot = __dirname + "/dist/xopstzweb/"; //di sesuaikan dengan path folder dist
app.set("port", process.env.PORT || 4200);
app.use(express.static(staticRoot));
app.use(function (req, res, next) {
// if the request is not html then move along
var accept = req.accepts("html", "json", "xml");
if (accept !== "html") {
return next();
}
// if the request has a '.' assume that it's for a file, move along
var ext = path.extname(req.path);
if (ext !== "") {
return next();
}
fs.createReadStream(staticRoot + "index.html").pipe(res);
});
app.listen(app.get("port"), function () {
console.log("app running on port", app.get("port"));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment