How to send multiple files and folders to local server express - node.js

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');
});

Related

Set new.html as the base file

I would like to set new.html as the base file for the server, but it keeps defaulting to index.html
My code is below...
const express = require('express');
const app = express();
// Set '/public' as the static folder.
app.use(express.static(__dirname + '/public'));
// Set index.html as the base file
app.get("/", function(req, res) {
res.sendFile(__dirname + '/new.html');
});
var server = app.listen(4000, function() {
console.log('Listening to requests on port 4000');
});
You can specify the index file to use as an option to express.static():
const express = require('express');
const app = express();
// Set '/public' as the static folder.
app.use(express.static(__dirname + '/public', {
index: 'new.html'
}));
const server = app.listen(4000, function() {
console.log('Listening to requests on port 4000');
});
Then, you no longer need the / route.
Alternatively, you could specify your / route before your static directory:
const express = require('express');
const app = express();
// Set new.html as the base file
app.get('/', function(req, res) {
res.sendFile(__dirname + '/public/new.html');
});
// Set '/public' as the static folder.
app.use(express.static(__dirname + '/public'));
const server = app.listen(4000, function() {
console.log('Listening to requests on port 4000');
});
Note: the file path should be /public/new.html rather than /new.html.

Node Js saving to a txt file not working

I have a server running on Node Js. What I'm doing is whenever the users submit something to save their input to a text file. When I run my server as a localhost it works and saves the input to the file. Whenever I run it on the real published server it doesn't. Is there a way to accomplish it on the real server without a database?
My code:
var fs = require('fs');
const log=require('simple-node-logger').createSimpleLogger();
var express = require('express');
var bodyParser = require('body-parser');
var app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
var port = process.env.PORT || 8081;
app.use(express.static(__dirname + '/server'));
app.use(express.static(__dirname + '/public'));
app.use('/images', express.static(__dirname +'/images'));
app.get('/', function(req, res){
res.sendFile('main.html');
});
app.listen(port, function(){
console.log('Server is running on port:' + port);
});
app.post('/submit', function(req, res){
var data = fs.writeFileSync('fileSync', req.body.rank, 'utf8');
return res.sendFile('success.html');
});
Thank you in advance!

How do I return an index.html file from express using the package 'express-https-redirect'

I have the following code which is defaulting to use HTTPS, I'm just unclear how to send an index.html file back
var express = require('express'),
path = require('path'),
fs = require('fs'),
app = express(),
staticRoot = __dirname + '/',
httpsRedirect = require('express-https-redirect');
app.set('port', (process.env.PORT || 3001));
app.use('/', httpsRedirect())
app.listen(app.get('port'), function () {
console.log('app running on port', app.get('port'));
});
Any help is greatly appreciated
You can send the html file usually asfollows
app.get('/', function(req, res) {
res.sendFile(path.join(__dirname + '/index.html'));
});
Note: express-https-redirect middleware redirects non-secure access to HTTPS Only.

openshift node.js Cannot Get /

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

socketio and nodejs, error 404 /socket.io/socket.io.js

I'm getting this error "NetworkError: 404 Not Found - http://localhost:3000/socket.io/socket.io.js" and I don't know how can I fix it. I have tried changing the port, adding the link with the port ... and I have no idea.
My app.js :
var express = require('express')
, http = require('http')
, stylus = require('stylus')
, nib = require('nib')
var app = express();
var server = http.createServer(app);
var io = require('socket.io').listen(server);
function compile(str, path) {
return stylus(str)
.set('filename', path)
.use(nib());
}
app.set('views', __dirname + '/views')
app.set('view engine', 'jade')
app.use(express.logger('dev'))
app.use(stylus.middleware(
{ src: __dirname + '/public'
, compile: compile
}
))
app.use(express.static(__dirname + '/public'))
app.get('/', function (req, res) {
res.render('index',
{ title : 'Home' }
)
})
app.listen(3000)
note: The console said it's ok ( info - socket.io started )
Anyway I have tried this
app.listen(app.get('port'), function(){
console.log("Express server listening on port " + app.get('port'));
});
but doesn't print the port, say me undefined
You are creating an express HTTP server as well as a standard HTTP server. The app in your example above refers to the Express app which does not have .get('port'); method, try using server.listen. For example:
var express = require('express');
var http = require('http');
var app = express();
var server = http.createServer(app);
app.get('/', function(req, res) {
res.send("Hello World!");
});
server.listen(3000);
console.log('Express server started on port %s', server.address().port);

Resources