0
votes

When i am trying to launch my nodejs server, it does not call the client/public folder. I get the below error, when i point to http://localhost:3000

Error: Not found at /home/shankar/projects/apiDocs/server/server.js:36:12 at Layer.handle [as handle_request] (/home/shankar/projects/apiDocs/node_modules/express/lib/router/layer.js:95:5) at trim_prefix (/home/shankar/projects/apiDocs/node_modules/express/lib/router/index.js:312:13) at /home/shankar/projects/apiDocs/node_modules/express/lib/router/index.js:280:7 at Function.process_params (/home/shankar/projects/apiDocs/node_modules/express/lib/router/index.js:330:12) at next (/home/shankar/projects/apiDocs/node_modules/express/lib/router/index.js:271:10) at /home/shankar/projects/apiDocs/node_modules/express/lib/router/index.js:618:15 at next (/home/shankar/projects/apiDocs/node_modules/express/lib/router/index.js:256:14) at Function.handle (/home/shankar/projects/apiDocs/node_modules/express/lib/router/index.js:176:3) at router (/home/shankar/projects/apiDocs/node_modules/express/lib/router/index.js:46:12)

Below is my server.js code

var express = require('express');
var path = require('path');
var logger = require('morgan');
var bodyParser = require('body-parser');
var mongoose = require('mongoose');
var app = express();

app.use(express.static(path.join(__dirname, "public")));    
app.use(logger('dev'));
app.use(bodyParser.json({limit: '50mb'}));

mongoose.connect('mongodb://127.0.0.1:27017/app', function (error) {
    if (error) {
        console.log(error);
    }
});

app.all('/*', function(req, res, next){
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Methods", "GET, PUT, POST, DELETE, OPTIONS");
    res.header("Access-Control-Allow-Headers", "Content-type,Accept,X-Access-Token,X-Key");

    if(req.method === 'OPTIONS'){
        res.status(200).end();
    } else {
        next();
    }

});

app.all("/api/v1/*", [require('./middlewares/validateRequest')]);
app.use("/", require("./routes"));

app.use(function(req, res, next){
    var err = new Error("Not found");
    err.status = 404;
    next(err);
});

app.set('port', process.env.PORT || 3000);
var server = app.listen(app.get('port'), function() {
    console.log('Express server listening on port ' + server.address().port);
});

I am trying to run my client and my restful services on same server, when i launch my server on port 3000, i expect the client and server to be on the same server.

Folder Structure:

/app
--node-modules
--public
   --app //contains all angularjs related files
   --assets //contains images and js libraries
   --index.html
--server
   --server.js
1

1 Answers

0
votes

try to use this :

app.use(express.static('./.'));

this is should let express able to see your files