0

I am using the Angular Seed project to build a simple website. When i start the node server and enter the url at localhost:8000, it serves up the directory contents. I would like it to serve up the index.html file but would like to do this without a redirect.

I believe that I need to modify the following function and that I should change the code for the isDirectory check but I'm not sure if that is the correct way to go about doing this. Any suggestions would be appreciated.

StaticServlet.prototype.handleRequest = function(req, res) {
    var self = this;
    var path = ('./' + req.url.pathname).replace('//','/').replace(/%(..)/g, function(match, hex){
        return String.fromCharCode(parseInt(hex, 16));
    });
    var parts = path.split('/');
    if (parts[parts.length-1].charAt(0) === '.')
        return self.sendForbidden_(req, res, path);
    fs.stat(path, function(err, stat) {
        if (err)
            return self.sendMissing_(req, res, path);
        if (stat.isDirectory())
            return self.sendDirectory_(req, res, path);
        return self.sendFile_(req, res, path);
    });
}

Update #1

I have two screenshots to clarify. The first image is what I currently get, the second image is what I want.

What I Get What I Get


What I Want What I Want


Update #2

Using the link to Restify below I found the following example which is exactly what I needed.

var server = restify.createServer();
var io = socketio.listen(server);

server.get('/', function indexHTML(req, res, next) {
    fs.readFile(__dirname + '/index.html', function (err, data) {
        if (err) {
            next(err);
            return;
        }

        res.setHeader('Content-Type', 'text/html');
        res.writeHead(200);
        res.end(data);
        next();
});


io.sockets.on('connection', function (socket) {
    socket.emit('news', { hello: 'world' });
    socket.on('my other event', function (data) {
            console.log(data);
    });
});

server.listen(8080, function () {
    console.log('socket.io server listening at %s', server.url);
});
1
  • This should also work to server static html: server.get("/", restify.serveStatic({ directory: './public', default: 'index.html' })); Commented Apr 13, 2013 at 8:26

1 Answer 1

2

The angular-seed project is a starting point for the client, but not really for the server side. They included a simple web-server node script without dependencies. This way you don't need npm or other modules.

For the server side you can use node with connect/express or any other web server/language. You just need to make rest services and serve some static html.

Since you have already installed Node restify may be something for you.

Update: I created a basic sample for using the angular-seed with restify:
https://github.com/roelandmoors/restify-angular-seed

Sign up to request clarification or add additional context in comments.

1 Comment

Great answer and you even provided a link! I even found a concrete example of what I need to do using Socket.IO. Thank you so much.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.