My project structure is
myApp-
|-dist -
|- myApp -
|- index.html
|- main.js
|-server-
|- app.js
|-src-
| - app
| - assets
| - index.html
| - main.ts
my app.js
file
var express = require('express');
var path = require('path');
var bodyParser = require('body-parser');
var http = require('http');
var app = express();
var port = process.env.PORT || 3000;
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));
app.use(express.static(path.join(__dirname, 'dist')));
app.get('/',(req,res)=> {
res.sendFile(path.join(__dirname, '../dist/myApp/index.html'));
});
var server = http.createServer(app);
server.listen(port, ()=>{
console.log('Server running at port ', port)
});
And Angular file is same as default
But it shows just an empty page. I know it is reaching to index.html
because it changes the name of the title as in index.html but it is not reaching to app.component.html
index.html page is
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>BtechApp</title>
<base href="/BtechApp">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" type="image/x-icon" href="favicon.ico">
</head>
<body>
<app-root></app-root>
</body>
</html>
and when I am inspecting that page it shows error
Uncaught SyntaxError: Unexpected token <
Also, I checked on net as path of index.html from app.js is
dist -
| - index.html
But in my Case path is
dist -
| - myApp -
| - index.html
NOTE - I found the problem was in my app.js file, You can check my answer below.
index.html has tag instead of the type of the document. So when node is trying to parse your index.html, it gets the first character as < and that is what it is complaining about. Putting html tag should help.
I was only giving the wrong path to index.html
so the new app.js will be as
app.use(express.static(__dirname + '/dist/newApp'));
app.get('/', (req,res)=>{
res.sendFile(path.join(__dirname));
});
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.