why wont my simple Apache node sever images render? - node.js

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

Related

node express returns "Cannot Get" other solutions/ code not working. file upload

tldr
Ok i've been trying to get a file upload server working for a few days now and evrything i try just returns cannot get.
i'm currently trying the setup below but it is not working
code
here is server.js
const express = require("express");
const upload = require("./upload");
const cors = require("cors");
var router = express.Router();
var app = express();
const server = express();
var corsOptions = {
origin: "*",
optionsSuccessStatus: 200
};
server.use(cors(corsOptions));
router.get("/", function(req, res) {
res.render("index", { title: "Express" });
});
server.post("/upload", upload);
const port = process.env.PORT || 8000;
app.listen(port, () => {
console.log(`listening on port ${port}`);
});
Below is upload.js
const IncomingForm = require("formidable").IncomingForm;
module.exports = function upload(req, res) { var form = new IncomingForm();
form.on("file", (field, file) => {
// Do something with the file
// e.g. save it to the database
// you can access it using file.path
console.log("thisno werk"); }); form.on("end", () => {
res.json(); }); form.parse(req); };
Let's mean server as the result of the const server = require('express')();, and router as the result of const router = require('express').Router();.
server is an instance of your Express server while router is an instance of API endpoints router. You don't only need to write your router router.get();, but you also need to set the appropriate files (so-called controllers) for handling API requests.
So your code should have this line: server.use('/', yourController); or simply server.get('/', handlingFunction); if you don't have API sections.
However, if you use routers, then you need the first variant.
Your POST /upload method works great because it's configured on the app level. But you need to fix your GET / method because it's configured on the router level that is unused in your app.
Source: Express routing

syntax error: unexpected token < after replace controller in AngularJS

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.

Rest Api with node.js localhost not answering

Hy there, I'm trying to learn to make a REST API. I have the following code, where I mention that i have no error. When I try to access localhost:3000 nothing happens it's just reloading a blank page. What am I doing wrong?
Servers.js
const http = require('http');
const app = require('./app').default;
const port = process.env.port || 3000;
const server = http.createServer();
server.listen(port);
App.js
const express = require('express');
const app = express();
app.use((req, res, next) => {
res.status(200).json({
message: 'It works!'
});
});
module.exports = app;
You've not defined any route. Express generator generates the boilerplate code which is use full to start with.
You can also define a route like this:
app.use('/', function (req, res, next) {
return res.render('index', {title: 'DASHBOARD', 'data': ''});
});
Have a look at this doc: https://expressjs.com/en/starter/generator.html

Can not post to server using node express

I am trying to post data from postman to my node server, I keep getting 404.
Is my code setup correctly to receive post to http://localhost:8080/back-end/test and if not how can I fix it ?
var express = require('express');
var request = require('request');
var nodePardot = require('node-pardot');
var bodyParser = require('body-parser');
var rp = require('request-promise');
var app = express();
var port = process.env.PORT || 8080;
// Start the server
app.listen(port);
app.use(bodyParser.json()); // support json encoded bodies
app.use(bodyParser.urlencoded({extended: true})); // support encoded bodies
console.log('Test server started! At http://localhost:' + port); // Confirms server start
var firstFunction = function () {
return new Promise (function (resolve) {
setTimeout(function () {
app.post('back-end/test.js', function (req, res) {
console.log(req.body);
var login = req.body.LoginEmail;
res.send(login);
resolve({
data_login_email: login
});
});
}, 2000);
});
};
I am posting LoginEmail and keep getting 404.
Move app.post() outside of the timeout, promise, and firstFunction.
There is no proceeding paths defined in your code, so the path must start with a /: /back-end/test.js. Don't forget the extension since you've defined it.

NodeJS - create express server on button click event

I'm beginner to Node, I just need to create a express server on a button click event, I did as below but it shouldn't work
script.js
$('#button').click(function(){
$.post('/test');
});
app.js
app.post('/test', function (req, res) {
var express = require('express');
var app = express();
app.get('/test', function (req, res) {
res.send('Hello World');
})
var server = app.listen(3100, function () {
var host = server.address().address
var port = server.address().port
})
});

Resources