My project has files, in the screenshot below. My task is to run the whole project on a local express server. But only index.html is displayed without styles. The static file method doesn't work either. How can I fix this?
screenshot
server.js code:
const express = require("express");
const app = express();
var http = require('http').Server(app);
app.use(express.static(__dirname + 'css'))
app.use(express.static(__dirname + 'img'))
app.use(express.static(__dirname + 'js'))
app.use(express.static(__dirname + 'sass'))
app.use(express.static(__dirname + 'vendor'))
app.get('/', function(req, res){
res.sendfile('index.html');
});
http.listen(3000, function(){
console.log('listening on 3000');
});
I'm new in Angularjs and i'm trying to create a basic one page app using ngRoute. But i have a problem. I wan't my user to get the index.html file everytime. But if the url is /page1 my server is returning the page1 so index.html isn't loading. I tryed to fix it by doing like that :
Angular file :
test.config(function($routeProvider, $locationProvider) {
$routeProvider
.when('/', {
templateUrl : '/page1',
controller : 'trapController'
})
.when('/test', {
templateUrl : '/page2',
controller : 'trapController'
});
$locationProvider.html5Mode(true);
});
Node.js file :
require('dotenv').load();
const express = require('express');
const app = express();
const port = process.env.PORT || 8080;
const bodyParser = require('body-parser');
const path = require('path');
const apirouter = require('./apirouter');
const viewrouter = require('./viewrouter');
app.use('/api', apirouter);
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(bodyParser.json());
app.use(express.static(__dirname + '/public'));
app.engine('html', require('ejs').renderFile);
app.use('/*', function(req, res){
res.sendFile(__dirname + '/public/index.html');
});
// START THE SERVER
// =============================================================================
app.listen(port, () => {
console.log('Server running on port ' + port);
});
But this way nothing is working.. It seems like every call is asking to the server and not looking into the public folder. So when i call for a .js file it load me a new index.html file... I don't know how to do.. Can you help me ? :)
I was apparently doing wrong with :
Better use this :
app.use('/', express.static(__dirname + '/public'));
Than this :
app.use(express.static(path.join(__dirname, 'public')));
I try to configure my node.js + express application for creation multiple domain server. This is my code, but unfortunately it's not working. Please explain me, what is wrong in this code?
const express = require('express');
const bodyParser = require('body-parser');
const path = require('path');
const vhost = require('vhost')
const app = express();
app.use(bodyParser.json());
// Port
const port = 8081;
app.use('/', express.static(path.join(__dirname, 'public')));
app.use(vhost('*.localhost:8081', function(req, res, next){
res.send('Ok, its work.');
}));
app.listen(port, function(){
console.log('App is running on port '+ port);
});
I have this simple node.js static file server:
var express = require('express');
var app = express();
app.use(express.cookieParser());
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.session({ secret: "secret" }));
app.use(express.static(process.env.OPENSHIFT_REPO_DIR + '/public' ));
var ipaddress = process.env.OPENSHIFT_NODEJS_IP || "127.0.0.1";
var port = process.env.OPENSHIFT_NODEJS_PORT || 8080;
app.listen(port, ipaddress);
When I publish to OpenShift (no errors in the terminal) and navigate to the site, I see Cannot GET /
I have index.html in my public folder, along with some css and JavaScript.
Any idea what I'm doing wrong?
Here it is: Hey, did you make sure to commit the public folder?
You need the load the routes.
var express = require('express');
var app = express();
app.get('/', function(req, res){
res.send('hello world');
});
app.listen(3000);
You are also not loading any views.
Here's the documentation of express: http://expressjs.com/guide.html
I am trying to print the post data on my console
app.js
var express = require('express')
, http = require('http');
var app = express();
app.set('port', process.env.PORT || 7002);
app.use(express.static(__dirname + '/public/images'));
app.post('/Details/',function(request,response,next){
app.use(express.bodyParser());
var keyName=request.query.Key;
console.log(keyName);
} );
http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});
Initial snapshot::
I test with POST-MAN with below data::
Now i get error as::
I just want to print the data i recieved from postman that is dev
..... which is being displayed as undefined ?
How to resolve this !
[Edit] ---- Adding body parser outside the route
var express = require('express')
, http = require('http');
var app = express();
app.set('port', process.env.PORT || 7002);
app.use(express.static(__dirname + '/public/images'));
app.use(express.bodyParser());
app.post('/Details/',function(request,response,next){
var keyName=request.query.Key;
console.log(keyName);
} );
http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});
Still have same error
Instead of query:
var keyName=request.query.Key;
console.log(keyName);
use body:
var keyName1=request.body.key;
console.log(keyName1);
Code:
var express = require('express')
, async = require('async')
, http = require('http');
var app = express();
app.set('port', process.env.PORT || 7002);
app.use(express.static(__dirname + '/public/images'));
app.use(express.bodyParser());
app.post('/Details/',function(request,response,next){
var keyName1=request.body.key;
console.log(keyName1);
} );
http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});
var express = require('express');
var app = express();
app.use(express.bodyParser());
app.post('/post/', function(req, res) {
// print to console
console.log(req.body);
// just call res.end(), or show as string on web
res.send(JSON.stringify(req.body, null, 4));
});
app.listen(7002);
Use request.query when you have querystring params.
For form/post data use req.body.
In your case, use request.body.key.
An update on using the middleware, body-parser, for later versions of Express: Using app.use(express.bodyParser()) will report an error such as:
Error: Most middleware (like bodyParser) is no longer bundled with Express and must be installed separately. Please see https://github.com/senchalabs/connect#middleware.
This can be addressed by first installing the body-parser middleware:
npm install body-parser
then write code such as:
var bodyParser = require('body-parser');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
and then accessing the body of the request object, for example, console.log(req.body)
You can't call app.use(express.bodyParser()); inside middleware/route handler:
request should pass through bodyParser() before it reaches route handler
you will be adding new bodyParser()s in each request, but they will be after app.router and will never work
Use built in function "util" to print any type of json data in express js
var util = require("util");
console.log(util.inspect(myObject, {showHidden: false, depth: null}));