syntax error: unexpected token < after replace controller in AngularJS - node.js

I use the sample of Auth0 examples AngularJS. I need to rewrite this sample and replace the controllers js file to a folder - controllers. But after doing that I get an error: syntax error: unexpected token <
I figure out that it is may server problem
server configuration on node. js is :
const express = require('express');
const app = express();
const path = require('path');
app.use('/', express.static(__dirname + '/'));
app.get('/*', function(req, res) {
res.sendFile(path.join(__dirname + '/index.html'));
});
const hostname = '0.0.0.0';
const port = 3000;
const server = app.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
What problem of this server? Thank you.

Related

Route handlers not working properly in express server (Create react app)

I have a simple react app made with CRA, and I deployed it on vercel. Everything seems to be working with one exception. My route handlers are not sending back the correct response. It seems like they are sending the index.html file that is created from the build script. I'm not really sure why this is happening. It sent the correct response when I built it locally. Here is the code for the express server:
const express = require("express");
const bodyParser = require("body-parser");
const path = require("path");
const app = express();
app.use(express.static(path.join(__dirname, "build")));
app.get("/ping", function (req, res) {
return res.send("pong");
});
// app.get("*", (req, res) => {
// res.sendFile(path.join(__dirname, "build", "index.html"));
// });
const PORT = process.env.PORT || 8080;
app.listen(PORT, () => {
console.log(`Server listening on ${PORT}`);
});
And here is the file structure if that helps:
I figured out the issue. Vercel only supports serverless deployment. You can't host an express server, just a static site

React router not working when navigating using url

I am getting an error Cannot GET /'page' when trying to navigate using url to a path in React Router. Navigating from application works perfectly fine. Here is my server.js:
const express = require('express');
const app = express();
const port = process.env.PORT || 3001;
const path = require('path');
app.use(express.static('build'));
app.listen(port, () => {
console.log('Server is up on port: ' + port);
})
I've tried adding this code snippet to my server but it didn't work:
app.get('/*', function(req, res) {
res.sendFile(path.join(__dirname, './public/index.html'), function(err) {
if (err) {
res.status(500).send(err)
}
})
})

find the error while deploying the node.js app to server

I found the error while deploying the node app on the server. the index.js file code
const connectToDb = require("./DBconfig");
const express = require('express')
const router = express.Router();
var cors = require('cors');
connectToDb();
const app = express();
const port = process.env.PORT | 5000;
app.use(cors());
app.use(express.json());
connectToDb();
app.use( '/api/user' ,require('./routes/user') );
app.use( '/api/post' ,require('./routes/post') );
app.get('/', (req, res) => {
res.send('The site is start now')
})
if(process.env.NODE_ENV === "production"){
app.use(express.static("client/build"));
const path = require("path");
app.get("*",(req,res)=>{
res.sendFile(path.resolve(__dirname,'client','build','index.html'))
})
}
app.listen(port, () => {
console.log(`Example app listening on port ${port}`)
})
when I deploy the app and install the node in the server like how I install the node in server I attached the picture in which I show the installation of the node. and above I add the index.js code. It works properly in the local machine but if I hit the URL of backend backendURL it show the service unavailable error message kindly give me the solution to solve this problem.

Node.js/Express redirect all to angular 2 page

I am creating an Angular 2 app with Node.js and Express.
The problem I am having is that my routes file doesnt work with a wildcard. Everytime I visit the page with anything other then / (for example /test) it says the following: ReferenceError: path is not defined
My server.js:
const express = require('express');
const app = express();
const path = require('path');
const routes = require('./routes');
const data = require('./articles.json');
app.use(express.static(path.join(__dirname, '/dist')));
app.use('/', routes);
app.listen(8080, function () {
console.log('App started on port 8080');
});
My /routes/index.js:
const routes = require('express').Router();
routes.get('*', function (req, res) {
res.sendFile(path.join(__dirname + '/dist/index.html'));
});
module.exports = routes;
So what am I doing wrong here?
You need to require the path package in your index.js too
/routes/index.js
const path = require('path');
const routes = require('express').Router();
routes.get('*', function (req, res) {
res.sendFile(path.join(__dirname + '/dist/index.html'));
});
module.exports = routes;

why wont my simple Apache node sever images render?

Why wont my code behave like an appache server? It rlinks and prints the html correctly but the images don't load correctly. What am I missing?
var express = require('express');
var app = express();
var path = require('path');
app.get('/:path', function(req, res) {
var p = path.join(__dirname,"/" + req.params.path)
res.sendFile(p);
});
app.get( '*', function(req, res) {
var p = path.join(__dirname, "/index.html")
res.sendFile(p);
});
app.listen('3000', function () {
console.log('app listening on port 3000!');
})
Allso I just noticed when I load javascript I get the error:
Uncaught SyntaxError: Unexpected token < //for each js file

Resources