if you are trying to look at it in the browser(which is the only place I found it not to work as expected), you could wrap each line in it's own HTML tag. Here is what I did to achieve the expected result when looking at the result in Chrome.
router.get("/", (req, res, next) => {
const { exec } = require("child_process");
exec(
`.\server\scripts\test.ps1`,
{ shell: "powershell.exe" },
(error, stdout, stderr) => {
// Split each line by the new line character,
// results in [ 'Line 1', 'Line 2', 'Line 3', '' ]
const splitLines = stdout.split("
");
// deletes last element in array, which is blank
splitLines.pop();
// use .map function to add HTML tag wrapper around each element in array
// use .join("") to transform array back to a single string
const convertToHTML = splitLines.map(line => {
return `<div>${line}<div>`
}).join("")
// now express serves up the string as HTML, so now the result is in multiple lines
res.send(convertToHTML);
}
);
});
Hope this solves your problem :)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…