3

I'm trying to start react-js app with node server. Structure of directory: MyTodo

---bin/

------css/

------fonts/

------js/

------index.html

---sources/

------App.jsx

------TodoGroup/

file server.js:

 var express=require('express');
var app=express();

app.set('port',(process.env.PORT||3000));

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

app.listen(app.get('port'), function() {
	console.log('Server started: http://localhost:'+app.get('port')+'/');
});

webpack.config:

var path = require('path');
var webpack = require('webpack');
var ExtractTextPlugin = require("extract-text-webpack-plugin");

var config = module.exports = {
    context: __dirname,
    entry: {
        'App': './sources/App.jsx',
    },

    output: {
        path: './bin/js',
        filename: '[name].js'
    },

    plugins: [
        new ExtractTextPlugin('../css/[name].css')
    ],

	devServer: {
        contentBase: ".",
        host: "localhost",
        port: 3000
    },
	
   module:{
        loaders:[   //загрузчики
            {
                test: /\.jsx?$/, // определяем тип файлов
                exclude: /(node_modules)/,
                loader: "babel-loader",
                query:{
                    presets:["es2015", "react"]
                }
            }
        ]
    }

    resolve: {
        extensions: ['', '.js', '.jsx']
    }
}

In browser http://localhost:3000/ shows an error: GET http://localhost:3000/ 404 (Not Found)

2
  • where is your server.js? Assuming it is in parent of bin/, then update the app.use('/',express.static(path.join(__dirname, "bin/"))); Import path too. Commented Jun 13, 2017 at 14:34
  • thank you, Molda's answer works Commented Jun 13, 2017 at 15:05

1 Answer 1

2

You don't have any route for handling http://localhost:3000/

You have only static file handler which means this url would probably work http://localhost:3000/index.html

If you want to access your web at http://localhost:3000/ then add this

app.get('/', function(req, res){
    res.sendFile(__dirname+'/bin/index.html'); // change the path to your index.html
});
Sign up to request clarification or add additional context in comments.

1 Comment

thank you, it works, but whith: res.sendFile(__dirname+'/bin/index.html');

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.